- iOS 13.0+
- Xcode 11.0+
- Core NFC
This sample code project shows how to use Core NFC in an app to read Near Field Communication (NFC) tags of types 1 through 5 that contains NFC Data Exchange Format (NDEF) data. To use this sample, download the project and build it using Xcode. Run the sample app on your iPhone. Tap the Scan button to start scanning for tags, then hold the phone near an NFC tag.
To read a tag, the sample app creates an NFC NDEF reader session and provides a delegate. The running reader session polls for NFC tags and calls the delegate when it finds tags that contain NDEF messages, passing the messages to the delegate. The delegate then stores the messages so the user can view them later.
Configure the App to Detect NFC Tags
Begin building your tag reader by configuring your app to detect NFC tags. Turn on Near Field Communication Tag Reading under the Capabilities tab for the project’s target (see Add a capability to a target). This step:
Adds the NFC tag-reading feature to the App ID.
Near Field Communication Tag Reader Session Formats Entitlementto the entitlements file.
Next, add the
NFCReader key as a string item to the
Info file. For the value, enter a string that describes the reason the app needs access to the device’s NFC reader. If the app attempts to read a tag without providing this key and string, the app exits.
Start a Reader Session
The reader session delegate object.
The dispatch queue to use when calling methods on the delegate.
invalidateflag to determine whether the reader session reads only a single tag or multiple tags.
After First Read
After creating the reader session, give instructions to the user by setting the
alert property. For example, you might tell users, “Hold your iPhone near the item to learn more about it.” The system displays this message to the user while the phone is scanning for NFC tags. Finally, call
begin to start the reader session. This enables radio-frequency polling on the phone, and the phone begins scanning for tags.
The sample app starts a reader session when the user taps the Scan button. The app configures the reader session to invalidate the session after reading the first tag. To read additional tags, the user taps the Scan button again.
Adopt the Reader Session Delegate Protocol
The reader session requires a delegate object that conforms to the
NFCNDEFReader protocol. Adopting this protocol allows the delegate to receive notifications from the reader session when it:
Reads an NDEF message
Becomes invalid due to ending the session or encountering an error
Read an NDEF Message
Each time the reader session retrieves a new NDEF message, the session sends the message to the delegate by calling the
reader method. This is the app’s opportunity to do something useful with the data. For instance, the sample app stores the message so the user can view it later.
Handle an Invalid Reader Session
When a reader session ends, it calls the delegate method
reader and passes in an error object that gives the reason for ending the session. Possible reasons include:
The phone successfully read an NFC tag with a reader session configured to invalidate the session after reading the first tag. The error code is
Session Invalidation Error First NDEFTag Read
An error occurred during the reader session. See
NFCReaderfor the complete list of error codes.
In the sample app, the delegate displays an alert when the reader session ends for any reason other than reading the first tag during a single-tag reader session, or the user canceling the session. Also, you cannot reuse an invalidated reader session, so the sample app sets
Write an NDEF Message
To write to a tag, the sample app starts a new reader session. This session must be active to write an NDEF message to the tag, so this time,
invalidate is set to
false, preventing the session from becoming invalid after reading the tag.
When the reader session detects a tag, it calls the
reader delegate method. However, because the session doesn’t become invalid after reading the first tag, it’s possible for the session to detect more than one tag. The sample app writes to one tag only, so it checks that the session detected only one tag. If the session detected more than one, the app asks the user to remove the tags, and then restarts polling to scan for a new tag.
After the app confirms that it has only one tag, it connects to the tag and verifies that it’s writable. The app then writes the NDEF message it read earlier to the tag.
Support Background Tag Reading
To learn how to set up your app to process tags that iOS reads in the background, see Adding Support for Background Tag Reading.