以下是实现它的方法。
但请记住,它使用私有函数,Apple 可以随时删除/更改该函数,并可能导致 AppStore 拒绝。
在我的应用中测试,它现在适用于 iOs 11.1
来源:
https://github.com/hansemannn/iOS11-NFC-Example/issues/16
https://github.com/chariotsolutions/phonegap-nfc/pull/287/files#diff-84fad93feff6a327c30a08cac8f546dfR171
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
let uid : String = getTagIdFromSession(session: session)
//Do what you want with the UID
}
func getTagIdFromSession(session : NFCNDEFReaderSession) -> String{
var uid: String = ""
if(session.value(forKey: "_foundTags") != nil) {
let foundTags : NSArray = session.value(forKey: "_foundTags") as! NSArray
if(foundTags.count > 0) {
let tag : NSObject = foundTags.firstObject as! NSObject;
if(tag.value(forKey: "_tagID") != nil) {
var uuidPadded : Data = tag.value(forKey: "_tagID") as! Data
//We reverse the order
for (i,_) in uuidPadded.enumerated() {
uuidPadded.insert(uuidPadded.remove(at:i),at:0)
}
for (_, element) in uuidPadded.enumerated() {
let tag : String = String(element, radix:16)
//We add the missing 0 in case the number is < 10. It can be done with bitwise operations too.
if(tag.length < 2) {
uid.append("0"+tag)
}
else {
uid.append(tag)
}
}
}
}
}
return uid;
}