【问题标题】:Swift: NFCError Code=202 "Session is invalidated unexpectedly"Swift:NFCError Code=202“会话意外失效”
【发布时间】:2020-01-12 22:41:58
【问题描述】:

我正在尝试使用 NFC。我遵循了这些步骤:

  • 在 AppID 配置中启用 NFC

  • 创建了一个配置文件并安装了它

  • 为目标添加了 NFC 功能

  • 在 plist 文件中添加了隐私描述

在此之后,我导入了 CoreNFC 并实现了这些代码:

@available(iOS 11.0, *)    
    extension EventPreviewViewController: NFCNDEFReaderSessionDelegate {
            func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
                let alert = UIAlertController.withOkButton(andTitle: NSLocalizedString("TitleWarning"), andText: NSLocalizedString("ErrorNFCInvalidate"), okHandler: nil)
                self.present(alert, animated: true, completion: nil)
            }

            func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
                // TODO
            }
        }


class EventPreviewViewController: UITableViewController {
@available(iOS 11.0, *)
var nfcSession: NFCNDEFReaderSession {
        return NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true)
    }

    @IBAction func startAccess(_ sender: UIButton) {
    if #available(iOS 11.0, *) {
                    nfcSession.begin()
                } else {
                    let alert = UIAlertController.withOkButton(andTitle: NSLocalizedString("TitleWarning"), andText: NSLocalizedString("ErrorNFCUnsupported"), okHandler: nil)
                    self.present(alert, animated: true, completion: nil)
                }
    }
}

为什么我不断收到“Error Domain=NFCError Code=202 "Session is invalidated unexpectedly" UserInfo={NSLocalizedDescription=Session is invalidated unexpectedly}”?

【问题讨论】:

  • 您是否断章取意地声明了nfcSession?我想是这样,因为显示的代码不会编译。您需要 nfcSession 成为属性,而不是局部常量,否则它将在其封闭函数返回后立即释放。
  • 我尝试将 nfcSession 设为属性(请参阅下面答案中的评论)但仍然无法正常工作...
  • edit 您的问题在上下文中显示相关的属性声明 - 您将它设为哪个对象的属性?该对象的生命周期是多少?消息说您的阅读器会话正在释放。

标签: ios swift nfc


【解决方案1】:

ios13 / swift 5.1 更新

a) Apple 的原始样本有时会出现同样的错误。

(https://developer.apple.com/documentation/corenfc/building_an_nfc_tag-reader_app)

b) 如果失败(似乎很愚蠢.. 无论如何.. 有效..)重启 设备。它发生了;(

【讨论】:

    【解决方案2】:

    我不确定,但在导致此错误的行下方Session is invalidated unexpectedly

    当我与CoreNFC 合作时,我遇到了类似的问题。通过定义为property来修复它

    let nfcSession = NFCNDEFReaderSession(delegate: self, queue: DispatchQueue(label: "queueName", attributes: .concurrent), invalidateAfterFirstRead: true)
    

    我建议您需要将nfcSession 定义为属性。

    var nfcSession: NFCNDEFReaderSession?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.nfcSession = NFCNDEFReaderSession(delegate: self, queue: DispatchQueue.global(qos: .background), invalidateAfterFirstRead: false)
        self.nfcSession?.begin()
        return true
    }
    

    更新:

    您可以为 iOS 11 定义一个属性,如下所示。

    @available(iOS 10.0, *)
        var session: NFCNDEFReaderSession?
    

    【讨论】:

    • 如何使该属性仅在 iOS11 或更高版本上可用? @available(iOS 11.0, *) var nfcSession: NFCNDEFReaderSession { return NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true) } 它不工作,我得到同样的错误。
    • 更新:存储的属性不能用“@available”标记为可能不可用
    猜你喜欢
    • 2019-11-14
    • 2011-08-13
    • 1970-01-01
    • 2013-04-14
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    相关资源
    最近更新 更多