Can't begin NFCNDEFReaderSession, invalidates immediately.

I'm trying to implement a simple app in Swift 4 with CoreNFC to explore the framework, but I can't get it to work on my iPhone 7.


My code is very straightforward:


nfcSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false)
nfcSession?.begin()
print("NFC Ready: \(nfcSession.isReady)")


And I always get the output:


NFC Ready: false


And then

func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error)

is called with the following error:


Error Domain=NFCError Code=202 "Session is invalidated unexpectedly" UserInfo={NSLocalizedDescription=Session is invalidated unexpectedly}


It doesn't matter when I call the functions, it always fails.


Does anyone have the same problem? Did you manage to solve it? Thanks!

For that error, it is likely due to not having proper Entitlements. You need to go to Apple, add the NFC entitlement to the app id, grab a provisioning profile/use that provisioning profile (you can click the (?) next to the label of the chosen profile under "Signing" in the "General" tab when you select the Target) that has the entitlement...AND you will need to manually add it to your app's .entitlements file (k:v = com.apple.developer.nfc.readersession.formats:NDEF).


After this, you should not get that error again. Instead, you will get, at least on iPhone 7, "Feature not supported" error 🙂

Thanks! So, I've made some progress. The docs say that if you instantiate another session, it puts them in a queue. So I've wrote my app with a "new session" button and I (gently) spam it. 😁


Eventually the scanner popup appears!! My tags don't work though hahaha



(I attached images but they're not showing properly for me.)


Interestingly, nfcSession.isReady never returns true? Or maybe begin() is blocking the thread. I will investigate tomorrow because it's 4 am haha. Good luck!


Edit: This is the exact code that when spammed triggers the scanner properly:


nfcSession?.invalidate() // nfcSession is a member variable
nfcSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false)
nfcSession?.begin()
print("NFC Ready: \(nfcSession?.isReady ?? false)")

i got the "Feature not supported" error too.

Does it mean my iPhone 7 is not support NFC? the document says “Reading NFC NDEF tags is supported on iPhone 7 and iPhone 7 Plus”.

I was able to get here by restaring the iPhone! But i can't read the tags, what might be the problem?


I am now at this point too, although I have written it in Objective C.


I don't see any errors when I do a


nfcSession.begin();


The scanning screen pops up on my iPhone 7 and looks to be working, not yet found anything I can actaully scan with it yet.

So I'm desparately searching for something with compatible NFC that I can interact with.


Plasma

For "Feature not Supported" seeming to be random:

Make sure the VALUE for the KEY 'com.apple.developer.nfc.readersession.formats' is an ARRAY with 'NDEF' in it. This will all likely become clearer if you visit the Lab @ WWDC or wait until the talk.

I don't see NDEF at the end of com.apple.developer.nfc.readersession.formats. I created the app ID with the correct entitlements and generated the profile and in the General tab, it still doesn't show the NDEF at the end of the entitlement string.


Should I still add an entitlements.plist to my project?

The session currently says it a manual process, you have to use the portal to add the entitlement then manually add it to your project lik @TikkaKai said.

Do you have a link to the session? I'm having trouble finding it.

Right after I said that I found it:


https://developer.apple.com/videos/play/wwdc2017/718/

No sample code as mentioned in the video, however.

Right under the video you see a link to "Resources". The sample code is there.

hi ... I am not getting scanning screen popup on my iPhone 7. I did all the prerequisites such as plist, entitlements, enable NFC in appID in dev account, etc.. but not sure where I am going wrong. Whereas I see sometimes Apple Demo App works well. Any clue, please?

I had the same problem, check if your entitlement file is configured in your target's build settings.

Can't begin NFCNDEFReaderSession, invalidates immediately.
 
 
Q