iOS CoreNFC-类"NFTechnologyEvent"未加载或不存在 [英] iOS CoreNFC - class "NFTechnologyEvent" not loaded or does not exist

查看:225
本文介绍了iOS CoreNFC-类"NFTechnologyEvent"未加载或不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用iPhone 7 Plus读取NFC标签时出现此错误

I'm getting this error when trying to read a NFC tag using an iPhone 7 Plus

2017-12-13 14:03:01.522137-0300 nfc [279:9534] [通用]与名为com.apple.nfcd.service.corenfc的服务的连接:解码接收到的消息期间捕获了异常,丢弃了传入消息.

2017-12-13 14:03:01.522137-0300 nfc[279:9534] [general] connection to service named com.apple.nfcd.service.corenfc: Exception caught during decoding of received message, dropping incoming message.

异常:解码参数0(调用的#2)时发生异常:

Exception: Exception while decoding argument 0 (#2 of invocation):

异常:decodeObjectForKey:类"NFTechnologyEvent"未加载或不存在

Exception: decodeObjectForKey: class "NFTechnologyEvent" not loaded or does not exist

我已经设置了适当的权利(Near Field Communication Tag Reading)和Privacy - NFC Scan Usage Description.

I have the proper entitlement (Near Field Communication Tag Reading) and Privacy - NFC Scan Usage Description setted.

要重现它,只需启动一个新的单视图项目,并用以下内容替换默认的ViewController:

To reproduce it just start a new single view project and replace the default ViewController with this:

import UIKit
import CoreNFC

class ViewController: UIViewController, NFCNDEFReaderSessionDelegate {
    // Reference the NFC session
    private var nfcSession: NFCNDEFReaderSession!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.startScanning()
    }

    func startScanning() {
        // Create the NFC Reader Session when the app starts
        self.nfcSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false)

        // A custom description that helps users understand how they can use NFC reader mode in your app.
        self.nfcSession?.alertMessage = "Macri Gato"

        self.nfcSession?.begin()
    }

    // Called when the reader-session expired, you invalidated the dialog or accessed an invalidated session
    public func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
        print("NFC-Session invalidated: \(error.localizedDescription)")

        print("==========================")

        self.startScanning()
    }

    // Called when a new set of NDEF messages is found
    public func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
        print("New NFC Messages (\(messages.count)) detected:")
        print(messages)
    }
}

推荐答案

在开始nfcSession之前,必须检查NFCNDEFReaderSession.readingAvailable true才能继续使用session.begin()方法.

Before begin nfcSession you have to check NFCNDEFReaderSession.readingAvailable true in order to go on session.begin() method.

还要确保在Info.plist中添加"隐私-NFC扫描使用说明"

Also make sure that you add "Privacy - NFC Scan Usage Description" into Info.plist

@IBAction func beginScanning(_ sender: Any) {
guard NFCNDEFReaderSession.readingAvailable else {
    let alertController = UIAlertController(
        title: "Scanning Not Supported",
        message: "This device doesn't support tag scanning.",
        preferredStyle: .alert
    )
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    self.present(alertController, animated: true, completion: nil)
    return
}

session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false)
session?.alertMessage = "Hold your iPhone near the item to learn more about it."
session?.begin()

}

这篇关于iOS CoreNFC-类"NFTechnologyEvent"未加载或不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆