簡體   English   中英

iOS CoreNFC - 類“NFTechnologyEvent”未加載或不存在

[英]iOS CoreNFC - class "NFTechnologyEvent" not loaded or does not exist

嘗試使用 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)
    }
}

在開始 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()

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM