iOS CoreNFC - 类“NFTechnologyEvent”未加载或不存在
Posted
技术标签:
【中文标题】iOS CoreNFC - 类“NFTechnologyEvent”未加载或不存在【英文标题】:iOS CoreNFC - class "NFTechnologyEvent" not loaded or does not exist 【发布时间】:2017-12-13 17:21:14 【问题描述】:我在尝试使用 iPhone 7 Plus 读取 NFC 标签时遇到此错误
2017-12-13 14:03:01.522137-0300 nfc[279:9534] [general] 连接到名为 com.apple.nfcd.service.corenfc 的服务:在解码收到的消息时捕获异常,丢弃传入的消息。
异常:解码参数 0 时出现异常(调用#2):
异常:decodeObjectForKey:类“NFTechnologyEvent”未加载或不存在
我已设置适当的权利 (Near Field Communication Tag Reading
) 和 Privacy - NFC Scan Usage Description
。
要重现它,只需启动一个新的单视图项目并将默认的 ViewController
替换为:
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)
【问题讨论】:
【参考方案1】:在开始 nfcSession 之前,您必须检查 NFCNDEFReaderSession.readingAvailable true 才能继续 session.begin() 方法。
还要确保将“隐私 - NFC 扫描使用说明”添加到 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”未加载或不存在的主要内容,如果未能解决你的问题,请参考以下文章
Apple Review - 未加载库:CoreNFC:找不到图像
当 TLV 字段使用 3 字节长度格式时,CoreNFC 不会读取