Adding Support for Background Tag Reading
Allow users to scan NFC tags without an app using background tag reading.
Overview
On iPhones that support background tag reading, the system scans for and reads NFC data without requiring users to scan tags using an app. The system displays a pop-up notification each time it reads a new tag. After the user taps the notification, the system delivers the tag data to the appropriate app. If the iPhone is locked, the system prompts the user to unlock the phone before providing the tag data to the app.
To avoid unintentional tag reading, the system reads tags in the background only when the user’s iPhone is in use. Also, be aware there are times when the display is on and background tag reading is unavailable, such as if:
The device has never been unlocked.
A Core NFC reader session is in progress.
Apple Pay Wallet is in use.
The camera is in use.
Airplane mode is enabled.
Process Scanned Tags
After the device scans an NFC tag while in background tag reading mode, the system inspects the tag’s NDEF message for a URI record by looking for an NFCNDEFPayload object with the following property values:
type equal to “U”
If the NDEF message contains more than one URI record, the system uses the first one. The URI record must contain either a universal link or a supported URL scheme.
Use Universal Links
For universal links, the system launches (or brings to the foreground) the app associated with the universal link after the user taps the notification. The system sends the NDEF message to the app as an NSUserActivity object. If there are no installed apps associated with the universal link, the system opens the link in Safari.
Use URL Schemes
The system processes NDEF payloads containing a URI for a URL scheme in the same way as universal links. The system displays a notification after reading the tag. When the user taps the notification, the system launches the app that supports the URL scheme.
Background tag reading supports the following URL schemes:
URL Scheme | Example |
|---|---|
Website URL (HTTP/HTTPS) | https://www.example.com |
mailto:user@example.com | |
SMS | sms:+14085551212 |
Telephone | tel:+14085551212 |
FaceTime | facetime://user@example.com |
FaceTime Audio | facetime-audio://user@example.com |
Maps | http://maps.apple.com/?address=Apple%20Park,Cupertino,California |
HomeKit Accessory Setup | X-HM://12345 |
For more information on URL schemes, see Apple URL Scheme Reference.
Configure Your App
Add support for background tag reading to your app by turning on Associated Domains under the project’s Capabilities tab. This step adds the Associated Domains Entitlement to your project’s entitlement file and to the app ID. Next, enter the domain for each universal link supported by your app.
[Image]
Handle Tag Delivery
To handle the NDEF message read from the tag, implement the application(_:continue:restorationHandler:) method in your app delegate. The system calls this method to deliver the tag data to your app in an NSUserActivity object. The user activity has an activityType of NSUserActivityTypeBrowsingWeb, and the tag data is available in the ndefMessagePayload property.
For user activities not generated by background tag reading, ndefMessagePayload returns a message that contains only one NFCNDEFPayload record. That record has a typeNameFormat of NFCTypeNameFormat.empty.
Listing 1. Process data from a tag read in the background
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb else {
return false
}
// Confirm that the NSUserActivity object contains a valid NDEF message.
let ndefMessage = userActivity.ndefMessagePayload
guard ndefMessage.records.count > 0,
ndefMessage.records[0].typeNameFormat != .empty else {
return false
}
// Send the message to `MessagesTableViewController` for processing.
guard let navigationController = window?.rootViewController as? UINavigationController else {
return false
}
navigationController.popToRootViewController(animated: true)
let messageTableViewController = navigationController.topViewController as? MessagesTableViewController
messageTableViewController?.addMessage(fromUserActivity: ndefMessage)
return true
}
Not all devices support background tag reading, so be sure to provide the user the option to read tags directly from your app. For more information, see Building an NFC Tag-Reader App.