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:

A record type (check out this doc) 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:

NFCNDEFReaderSession → a reader able to read data compatible with the NDEF protocol; 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:

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: