Skip to content
Language: EN

How does an iOS app receive a tag read in the background?

Through a universal link. On iPhones that support background reading, the system reads the tag, takes the first URI record and shows a notification. When the user taps it, the app that claims the link opens and receives the whole NDEF message in an NSUserActivity. Custom URL schemes do not work, and without the app the link opens in Safari.

What the system does

Apple's background reading article describes the sequence. The system reads the tag without any app open, shows a notification for each new tag, and delivers the data only after the user taps it; on a locked iPhone, it first asks the user to unlock.

It looks for one thing in the message: a URI record, of the well known type U. When the message holds several, the system uses the first, and that record must contain either a universal link or one of the URL schemes it supports.

Apple states that the iPhone XS and later support background tag reading. On older models the app has to offer reading from inside the app, which the article recommends in every case.

The article is explicit: background reading does not support custom URL schemes, and apps should use universal links instead. The schemes it does handle belong to the system: web links, mail, SMS, telephone, FaceTime, Maps and HomeKit accessory setup.

So the tag carries an https address on a domain the app claims. The app turns on Associated Domains and lists that domain, which adds the associated domains entitlement to the project and to the App ID.

When no installed app claims the link, the system opens it in Safari. That is the fallback the app opening page recommends designing for: a page that makes sense without the app.

What the app receives

The system launches the app, or brings it to the foreground, and calls application(_:continue:restorationHandler:) with an NSUserActivity of type NSUserActivityTypeBrowsingWeb. The message read from the tag is in its ndefMessagePayload property.

Check that the activity came from a tag. For activities not produced by background reading, the article says the payload holds a single record of the empty type, so an app that also opens its universal links from elsewhere can tell the two apart.

The whole message arrives, not only the link. Other records on the tag, a text or a MIME payload after the URI, are available to the app once it opens.

When it does not happen

Background reading runs only while the iPhone is in use, and the article lists moments when it is unavailable even with the display on: before the first unlock, during a Core NFC reader session, while Apple Pay Wallet is in use, while the camera is in use, and in airplane mode.

An app reading with its own session therefore blocks background reading for as long as the session lasts. The user side of these conditions is on the background reading page.

Frequently asked questions

Can a custom URL scheme open my app from a tag?

Not through background reading, which Apple's documentation says does not support custom URL schemes. Use a universal link on a domain the app claims.

Does the app open without the user tapping anything?

No. The system shows a notification for each new tag and delivers the data only after the user taps it, asking for an unlock first when the phone is locked.

Which record does the system use?

The first URI record in the message, of the well known type U. The whole message is then delivered to the app in the user activity.

What if the app is not installed?

The system opens the link in Safari, so the address should lead to a page that makes sense without the app.