CoreNFC tutorial

Well, when Apple presented iBeacon during the WWDC 2013, I bet that one year later they would have presented the NFC support. But it’s well known that I’m a terrible punter and Apple has finally introduce the support to NFC this year.

This means that starting from iOS 11 (and on iPhone 7/iPhone7+ and future deveices) our apps can leverage on NFC tags to get contextual info about the physical world around the user (especially for location aware apps, physical marketing campaigns, ..).


As described in the documentation:

Using Core NFC, you can read Near Field Communication (NFC) tags of types 1 through 5 that contain data in the NFC Data Exchange Format (NDEF)

So this means that so far it’s not possible to write NFC tags or card emulation.

NDEF

NDEF (NFC Data Exchange Format) is a protocol provided by the NFC Forum which is used to describe how a NFC tag must expose data. Every NDEF record is made up by two components:

  1. A record type (check out this doc)
  2. Payload data

Once a device (i.e. an iPhone or an Apple Watch) is ready to read data from a NFC tag and it’s placed near the tag, these components are exchanged between the NFC tag and the reader.

CoreNFC is able to interact with NFC tags 1/2/3/4 and 5.

Unfortunately this format is not fully supported because it seems that records like Mime type or URI are not included in the framework (or maybe we have to create custom classes to support them? 🤔)

Let’s code!

Consider that the CoreNFC framework doesn’t contain any slice for the simulator architecture! This means that you can try it only on real devices (only 7 and 7+ ☹️).

Honestly the API is quite easy and it’s mainly based on:

  1. NFCNDEFReaderSession → a reader able to read data compatible with the NDEF protocol;
  2. NFCNDEFReaderSessionDelegate → a collection of callbacks based on the reader status;

At 1 we are instantiating the reader able to read the NDEF format and setting the NFCViewController as delegate for the NFC callbacks.

An important parameter is invalidateAfterFirstRead: if it’s set to true, the app is able to read a single message otherwise if this is set to false then the app is able to read multiple messages during its lifecycle.

The last step is to implement the two required methods of the protocol NFCNDEFReaderSessionDelegate:

  1. handling an invalidated session

2. receive NDEF messages

3. stop reader session

System Level configurations

As described in the Apple doc:

To enable your app to detect NFC tags, visit Certificates, Identifiers, and Profiles.

You have to enable the option NFC Tag reading on your app id.

Then we have to ask the permission to the user:

<key>NFCReaderUsageDescription</key>
<string> Ready to use NFC 🚀 </string>

and finally add these to the entitlements file:

<key>com.apple.developer.nfc.readersession.formats</key>
<array>
<string>NDEF</string>
</array>

Conclusion

Well the API so far is really easy to use and with few functionalities.

What is not really clear to me is why the iPhone 6/6+ don’t support CoreNFC even though both support NFC.

So I really think I’m an awful punter but I predict that NFC will be able to write tags starting from iOS12, vero Giordano? :D


Feel free to contact me on Twitter for any comment. I’d love to hear any kind of feedback! ✌🏼

Thanks for reading! 👽

EDIT: A common issue

  1. Some developers told me that they weren’t able to retrieve messages from NFC Tags. The most common problem is that you have to be sure that you are using Tags with NDEF messages written on them. 😉