我想使用iOS 11 中引入的Core NFC 框架从我的 NFC-Tag (NXP NTAG213) 扫描 NDEF 数据。
我成功读取了标签中的有效负载:
TNF=1, Payload Type=<54>, Payload ID=<>, Payload=<026a61e3 8193e382 93e381ab e381a1e3 81af0a>
我想提取有效载荷部分,所以这是我尝试过的:
print("payload.payload")
但发生错误。
这是我的源代码:
import UIKit
import CoreNFC
class ViewController: UIViewController, NFCNDEFReaderSessionDelegate{
var payloadData = "HaveNoData"
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
print("エラーは発生しませんでした")
for message in messages{
for payload in message.records {
print (payload)
payloadData = String(describing: payload.payload)
}
}
print(payloadData)
}
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
print("Error: \(error.localizedDescription)")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let session:NFCNDEFReaderSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true)
session.begin()
}
@IBAction func launch(_ sender: Any) {
let session:NFCNDEFReaderSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true)
session.begin()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}