Getting Started with CoreNFC
With the introduction of iOS11 Apple has finally opened up the NFC Reader for third party developers (yaay). I had a look at the corresponding CoreNFC API in iOS11 Beta 1 and managed to get the whole thing working. As the documentation is still missing some vital details on how to setup your Xcode Project I have put together a quick tutorial to help you getting started.
What you need
At the moment CoreNFC only supports iPhone 7 and iPhone 7 Plus devices running iOS11 Beta 1 and Xcode 9 Beta 1. You also need to be a member of the Apple Developer Program.
Please Note: You cannot run apps that use CoreNFC in Simulator at the moment (you will get a ‘No such module’ error).
NFC Tag Reading Permission
God knows why but Apple requires you to add the ‘NFC Tag Reading’ permission to your app id on the Apple Developer Portal.
If you do not have a dedicated app id for your app you need to create one. Therefore, head over to the Apple Developer Portal and create a new App Identifier with the aforementioned permission. Create a corresponding Provisioning Profile as well.
This was probably the trickiest part to figure out to get the whole thing working. If you want to use CoreNFC you need to have a Code Signing Entitlement file setup for your project. Unfortunately, Xcode 9 Beta 1 does not provide a UI to set the required entitlement (yet). Thus, we need to do that manually.
Head over to your project and create a new file with the extension
MyApp.entitlements. Then open the file with a text editor and add the following content:
Then head over to your build settings and set the value for
Code Signing Entitlements to the path of the aforementioned file.
NFCNDEFReaderSession and Delegate
Finally, we can start implementing our NFC reader source code. The API itself is straight forward:
- Implement the
- Create an instance of
NFCNDEFReaderSessionand pass in the delegate
NFCNDEFReaderSessionobject and wait for the callbacks.
Here is a quick and dirty sample implementation in the AppDelegate:
You can find a complete sample application on my GitHub account.
NFC Usage Descriptor
We are almost there. Head over to your Info.plist and add the
Privacy - NFC Scan Usage Description key with a String value, e.g., “BlaBla”. This String will show up in the NFC system UI when you start an
Reading NDEF NFC Tags
Now that we have everything put together we can finally read NFC tags. Just run the app and you should see the following overlay on the screen:
Now hold your phone close to a compatible NDEF tag (the actual NFC antenna is in the upper part of the iPhone 7 and 7 Plus) and you should both get a callback on the
readerSession(_:didDetectNDEFs:) method on your delegate and the UI should change as follows:
That’s it. Happy NFCing!
Note: I could not find any compatible NDEF NFC tag on any of my credit cards, id cards, member cards etc. Therefore, I had to build my own tag using a Nordic Semi NRF52832 development kit. I can provide the sources or hex file in case anyone needs assistance. Hopefully, Apple will allow a wider range of NFC tags in futures releases - or even emulating cards.