簡體   English   中英

Swift:NFCError Code=202“會話意外失效”

[英]Swift: NFCError Code=202 "Session is invalidated unexpectedly"

我正在嘗試使用 NFC。 我按照以下步驟操作:

  • 在 AppID 配置中啟用 NFC 應用 ID 配置

  • 創建一個配置文件並安裝它供應

  • 向目標添加了 NFC 功能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)
                }
    }
}

為什么我不斷收到“錯誤域=NFCError Code=202“會話意外失效” UserInfo={NSLocalizedDescription=Session 意外失效}”?

更新 ios13/swift 5.1

a) 來自 Apple 的原始樣本有時會因相同的錯誤而失敗。

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

b)如果失敗(似乎很愚蠢......無論如何......工作......)重啟設備。 它發生了;(

我不確定,但在導致此錯誤的行下方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?

暫無
暫無

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

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