CoreNFC扫描错误-会话意外无效-iOS 13.2

问题描述 投票:-2回答:1

我正在尝试通过CoreNFC框架读取NFC标签,但出现错误。已经尝试使用以下代码


import UIKit
import CoreNFC

class ViewController: UIViewController, NFCNDEFReaderSessionDelegate {

    @IBOutlet weak var messageLabel: UILabel!
    var nfcSession: NFCNDEFReaderSession?

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func scanPressed(_ 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
        }

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

    }

    func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
        // Check the invalidation reason from the returned error.
        if let readerError = error as? NFCReaderError {
            // Show an alert when the invalidation reason is not because of a
            // successful read during a single-tag read session, or because the
            // user canceled a multiple-tag read session from the UI or
            // programmatically using the invalidate method call.
            if (readerError.code != .readerSessionInvalidationErrorFirstNDEFTagRead)
                && (readerError.code != .readerSessionInvalidationErrorUserCanceled) {
                let alertController = UIAlertController(
                    title: "Session Invalidated",
                    message: error.localizedDescription,
                    preferredStyle: .alert
                )
                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                DispatchQueue.main.async {
                    self.present(alertController, animated: true, completion: nil)
                }
            }
        }

        // To read new tags, a new session instance is required.
        self.nfcSession = nil
    }
    func readerSessionDidBecomeActive(_ session: NFCNDEFReaderSession) {
        print ("readerSessionDidBecomeActive")

    }
    func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
        // Parse the card's information
        /*
            ( // Payload one (There's only one payload in this card)
                "TNF=1, /* Type Name Format */
                Payload Type=<55>,
                Payload ID=<>,
                Payload=<0048656c 6c6f21>" /* What we're really interested in */
            )
         */

        var result = ""
           for payload in messages[0].records {
               result += String.init(data: payload.payload.advanced(by: 3), encoding: .utf8)! // 1
           }

           DispatchQueue.main.async {
               self.messageLabel.text = result
           }

    }
}

控制台日志:

Started scanning for tags
2019-12-30 17:35:33.709179+0530 nfcTest[2492:251252] [xpc.exceptions] <NSXPCConnection: 0x281c92e20> connection to service on pid 82 named com.apple.nfcd.service.corenfc: Exception caught during decoding of received selector didDetectExternalReaderWithNotification:, dropping incoming message.
Exception: Exception while decoding argument 0 (#2 of invocation):
Exception: decodeObjectForKey: class "NFFieldNotification" not loaded or does not exist
(
    0   CoreFoundation                      0x000000018c7e5820 FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 1226784
    1   libobjc.A.dylib                     0x000000018c50dfa4 objc_exception_throw + 56
    2   Foundation                          0x000000018ccd1390 3C682044-3249-3938-8111-F2F12D066D2B + 2339728
    3   Foundation                          0x000000018caaa4e4 3C682044-3249-3938-8111-F2F12D066D2B + 83172
    4   Foundation                          0x000000018ccf35dc 3C682044-3249-3938-8111-F2F12D066D2B + 2479580
    5   Foundation                          0x000000018caff62c 3C682044-3249-3938-8111-F2F12D066D2B + 431660
    6   Foundation                          0x000000018caff994 3C682044-3249-3938-8111-F2F12D066D2B + 432532
    7   Foundation                          0x000000018cae9d70 3C682044-3249-3938-8111-F2F12D066D2B + 343408
    8   Foundation                          0x000000018cccf1bc 3C682044-3249-3938-8111-F2F12D066D2B + 2331068
    9   libxpc.dylib                        0x000000018c3b3448 219DEC3C-E62E-395D-8911-FB74F73A5598 + 50248
    10  libxpc.dylib                        0x000000018c3b37c0 219DEC3C-E62E-395D-8911-FB74F73A5598 + 51136
    11  libdispatch.dylib                   0x0000000100d4ec98 _dispatch_client_callout4 + 16
    12  libdispatch.dylib                   0x0000000100d68aa8 _dispatch_mach_msg_invoke + 420
    13  libdispatch.dylib                   0x0000000100d55990 _dispatch_lane_serial_drain + 304
    14  libdispatch.dylib                   0x0000000100d698f8 _dispatch_mach_invoke + 520
    15  libdispatch.dylib                   0x0000000100d55990 _dispatch_lane_serial_drain + 304
    16  libdispatch.dylib                   0x0000000100d56718 _dispatch_lane_invoke + 500
    17  libdispatch.dylib                   0x0000000100d61adc _dispatch_workloop_worker_thread + 1324
    18  libsystem_pthread.dylib             0x000000018c502f88 _pthread_wqthread + 276
    19  libsystem_pthread.dylib             0x000000018c505ad4 start_wqthread + 8
)
(
    0   CoreFoundation                      0x000000018c7e5820 FF63481D-FB6A-353B-B2C1-AC4EAECF594D + 1226784
    1   libobjc.A.dylib                     0x000000018c50dfa4 objc_exception_throw + 56
    2   Foundation                          0x000000018ccf3828 3C682044-3249-3938-8111-F2F12D066D2B + 2480168
    3   Foundation                          0x000000018caff62c 3C682044-3249-3938-8111-F2F12D066D2B + 431660
    4   Foundation                          0x000000018caff994 3C682044-3249-3938-8111-F2F12D066D2B + 432532
    5   Foundation                          0x000000018cae9d70 3C682044-3249-3938-8111-F2F12D066D2B + 343408
    6   Foundation                          0x000000018cccf1bc 3C682044-3249-3938-8111-F2F12D066D2B + 2331068
    7   libxpc.dylib                        0x000000018c3b3448 219DEC3C-E62E-395D-8911-FB74F73A5598 + 50248
    8   libxpc.dylib                        0x000000018c3b37c0 219DEC3C-E62E-395D-8911-FB74F73A5598 + 51136
    9   libdispatch.dylib                   0x0000000100d4ec98 _dispatch_client_callout4 + 16
    10  libdispatch.dylib                   0x0000000100d68aa8 _dispatch_mach_msg_invoke + 420
    11  libdispatch.dylib                   0x0000000100d55990 _dispatch_lane_serial_drain + 304
    12  libdispatch.dylib                   0x0000000100d698f8 _dispatch_mach_invoke + 520
    13  libdispatch.dylib                   0x0000000100d55990 _dispatch_lane_serial_drain + 304
    14  libdispatch.dylib                   0x0000000100d56718 _dispatch_lane_invoke + 500
    15  libdispatch.dylib                   0x0000000100d61adc _dispatch_workloop_worker_thread + 1324
    16  libsystem_pthread.dylib             0x000000018c502f88 _pthread_wqthread + 276
    17  libsystem_pthread.dylib             0x000000018c505ad4 start_wqthread + 8
)
Session did invalidate with error: Error Domain=NFCError Code=202 "Session invalidated unexpectedly" UserInfo={NSLocalizedDescription=Session invalidated unexpectedly}

参考链接:

任何帮助,我们将不胜感激。预先感谢。

ios swift nfc
1个回答
0
投票

您是否在plist文件中授予权限?

隐私-NFC扫描使用说明

© www.soinside.com 2019 - 2024. All rights reserved.