首页 > 解决方案 > NFCCore iOS 问题

问题描述

我使用 NFC 在 iOS 中开发了一个应用程序,但我总是收到错误 202,这意味着“意外错误”,但 Apple 没有提供有关此代码的任何信息。

我有:

  1. 在开发者 Apple 应用页面上启用 NFC
  2. 将 iPhone 7 与 iOS 11 一起使用
  3. 将 .entitlements 键 com.apple.developer.nfc.readersession.formats 设置为带有 NDEF 的数组
  4. 将 NFC 隐私密钥设置为 info.plist
  5. 编写管理 NFC 的代码(类似于此iOS CoreNFC - 类“NFTechnologyEvent”未加载或不存在

类 TPNfcReaderViewController : UIViewController {

var nfcSession: NFCNDEFReaderSession!
func startScanning(){
    nfcSession = NFCNDEFReaderSession(delegate: self, queue: DispatchQueue.main, invalidateAfterFirstRead: false)
    nfcSession.alertMessage = "You can hold you NFC-tag to the back-top of your iPhone"
    nfcSession.begin()
}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    startScanning()
}

扩展 TPNfcReaderViewController:NFCNDEFReaderSessionDelegate {

func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
    print("Error reading NFC: \(error.localizedDescription)")
    //session.invalidate()
    //self.startScanning()
}
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
    //Never called
}

}

但我总是得到

    connection to service named com.apple.nfcd.service.corenfc: Exception caught during decoding of received message, dropping incoming message.
Exception: Exception while decoding argument 0 (#2 of invocation):
Exception: decodeObjectForKey: class "NFTechnologyEvent" not loaded or does not exist

你有什么建议吗?

标签: iosnfc

解决方案


在调用 session.begin() 方法之前,您必须检查 NFCNDEFReaderSession.readingAvailable 是否为真。

还要确保将“隐私 - NFC 扫描使用说明”添加到 Info.plist

请查看以下苹果文档中的示例代码。https://developer.apple.com/documentation/corenfc/building_an_nfc_tag-reader_app


推荐阅读