Swift: NFCError Code=202 “Session is invalidated unexpectedly”

核能气质少年 提交于 2020-06-14 07:20:19

问题


I'm trying to use NFC. I followed those steps:

  • Enabled NFC in the AppID configuration

  • Created a provisioning profile and installed it

  • Added NFC capability to the target

  • Added the privacy description in the plist file

After this I imported CoreNFC and implemented those code:

@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)
                }
    }
}

Why I keep getting "Error Domain=NFCError Code=202 "Session is invalidated unexpectedly" UserInfo={NSLocalizedDescription=Session is invalidated unexpectedly}"?


回答1:


update for ios13 / swift 5.1

a) original sample from Apple does sometimes fails the same error.

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

b) if fails (seems a be stupid.. anyway.. works..) reboot device. It happens ;(




回答2:


I am not sure but below line causing this error Session is invalidated unexpectedly

When I was worked with CoreNFC, I was faced similar kind of issue. Fix it by defining as property

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

I suggest you need to define nfcSession as property.

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
}

Update:

You can define a property for iOS 11 like below.

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


来源:https://stackoverflow.com/questions/56681258/swift-nfcerror-code-202-session-is-invalidated-unexpectedly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!