I'm diving into iOS 13's new CoreNFC capabilities, and I'm struggling to get NFCTagReaderSession working. After setting up my entitlements and instantiating an NFCTagReaderSession and delegate I attempt to start the session by calling nfcTagReaderSession?.begin()
. My session immediately gets invalidated with this error:
Error Domain=NFCError Code=2 "Missing required entitlement" UserInfo={NSLocalizedDescription=Missing required entitlement}
I followed the documentation here for my entitlements file: https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_nfc_readersession_formats
I have also added the appropriate "Privacy - NFC Scan Usage Description" in my Info.plist.
Has anyone gotten this to work yet? Is this just a problem with Xcode 11 or iOS 13?
Here is the code in my ViewController:
import UIKit
import CoreNFC
class ViewController: UIViewController {
var nfcTagReaderSession: NFCTagReaderSession?
override func viewDidLoad() {
super.viewDidLoad()
nfcTagReaderSession = NFCTagReaderSession(pollingOption: [.iso14443, .iso15693, .iso18092], delegate: self)
nfcTagReaderSession?.begin()
print("isReady: \(nfcTagReaderSession?.isReady)")
}
}
extension ViewController: NFCTagReaderSessionDelegate {
func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {
print("Tag reader did become active")
}
func tagReaderSession(_ session: NFCTagReaderSession, didInvalidateWithError error: Error) {
print("\(error)")
}
func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
print("\(tags)")
}
}
Here is my entitlements file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
<string>TAG</string>
<string>NDEF</string>
</array>
</dict>
</plist>