Debugging CoreNFC not working
How to programmatically work CoreNFC on xcode-9
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
//What I need to do here
}
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
//What I need to do here
}
override func viewDidLoad() {
super.viewDidLoad()
let sessionReader = NFCNDEFReaderSession.init(delegate: self, queue: nil, invalidateAfterFirstRead: true)
let nfcSession = NFCReaderSession.self
let nfcTag = NFCTagCommandConfiguration.init()
let tagType = NFCTagType(rawValue: 0)
sessionReader.begin()
}
I want to know what I need to do to read some NFC tag.
+3
source to share
1 answer
There are four steps to make it work:
- Add NFC tag permission to your App ID in the Apple Developer Portal.
- Add a code signing file to your project and create parameters and add the following raw key and value:
- Add a usage description to your Info.plist:
-
Inject a delegate and pass it to
NFCNDEFReaderSession
init like this:import UIKit import CoreNFC @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, NFCNDEFReaderSessionDelegate { var window: UIWindow? var session: NFCNDEFReaderSession? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false) self.session?.begin() return true } func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) { print(error) } func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) { print(messages) }
}
+10
source to share