[英]Swift: NFCError Code=202 "Session is invalidated unexpectedly"
I'm trying to use NFC.我正在尝试使用 NFC。 I followed those steps:
我按照以下步骤操作:
After this I imported CoreNFC and implemented those code:在此之后,我导入了 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)
}
}
}
Why I keep getting "Error Domain=NFCError Code=202 "Session is invalidated unexpectedly" UserInfo={NSLocalizedDescription=Session is invalidated unexpectedly}"?为什么我不断收到“错误域=NFCError Code=202“会话意外失效” UserInfo={NSLocalizedDescription=Session 意外失效}”?
update for ios13 / swift 5.1更新 ios13/swift 5.1
a) original sample from Apple does sometimes fails the same error. a) 来自 Apple 的原始样本有时会因相同的错误而失败。
( https://developer.apple.com/documentation/corenfc/building_an_nfc_tag-reader_app ) ( https://developer.apple.com/documentation/corenfc/building_an_nfc_tag-reader_app )
b) if fails (seems a be stupid.. anyway.. works..) reboot device. b)如果失败(似乎很愚蠢......无论如何......工作......)重启设备。 It happens ;(
它发生了;(
I am not sure but below line causing this error Session is invalidated unexpectedly
我不确定,但在导致此错误的行下方
Session is invalidated unexpectedly
When I was worked with CoreNFC
, I was faced similar kind of issue.当我与
CoreNFC
一起工作时,我遇到了类似的问题。 Fix it by defining as property
通过定义为
property
修复它
let nfcSession = NFCNDEFReaderSession(delegate: self, queue: DispatchQueue(label: "queueName", attributes: .concurrent), invalidateAfterFirstRead: true)
I suggest you need to define nfcSession
as property.我建议您需要将
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
}
You can define a property for iOS 11 like below.您可以为 iOS 11 定义一个属性,如下所示。
@available(iOS 10.0, *)
var session: NFCNDEFReaderSession?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.