WWDC 2017 feature mini-demos: NFC
We wrap up our tests of the best iOS 11 features announced at WWDC. In this post, we try out the long-awaited NFC capabilities.
Each year, our developers dive into the fresh material announced during Apple’s WWDC. In this series of mini-articles, we wanted to share our take on a few of the best new iOS 11 features!
NFC, or Near Field Communication, is a technology with a ton of possibilities, and has been available on mobile phones for quite some time. Using NFC, you can wirelessly transfer data from one device to another when they’re close together.
However, NFC was not available on iPhones – Apple included the physical capability, but only allowed it to be used for its own Apple Pay.
Be that as it may, this changes with iOS 11: NFC is now available on iPhone 7 and iPhone 7 Plus! Simply create an AppID in the Apple Developer Center, enable ‘NFC Tag Reading’ for your AppID and you can get started.
An important drawback to take note of is the fact that background scanning, like on Android, is not supported. On Android, you can just touch the NFC tag with your phone, and the system will check which apps can do something with that data. On iOS, on the other hand, you’ll have to open the app you want to use to scan the tag first. You’ll then usually have to perform another action (pressing a button) to actually start scanning. This will bring up a UI layer in a pop-over – meaning you have to wait until the scanning is complete before doing anything else.
This has clearly been done for security reasons – no background reading means no one can abuse that right – but it still makes for a clunkier, less user-friendly UX and requires you to complete more actions than on Android.
Another difference with NFC on Android is that iOS’s support is read-only for now – an unfortunate limitation, although it still opens up quite a few interesting use cases.
One feature that immediately came to mind is an improvement to the Happy Caps app we built for Coca-Cola. With Happy Caps, the traditional Coca-Cola vending machine gets a companion app on users’ smartphones, allowing for interesting marketing opportunities like games, loyalty, coupons, mobile payment, etc. With NFC capabilities on iPhones, we could replace the current QR code users have to scan on their phones with an NFC tag. The same result in fewer taps!
If you’d like to experiment with similar features, let us walk you through it!
The first step is to enable the NFC capability in your project. Eventually, you’ll be able to go to
<projectName> > Capabilities to enable the NFC capability, but this won’t be available until later this year.
The workaround, if you want to test things now, is to add the capability manually to the entitlements file, by adding a specific key:
<key>com.apple.developer.nfc.readersession.formats</key> <array> <string>NDEF</string> </array>
The next step is adding a usage string to the project. In Info.plist we add the following key:
Privacy - NFC Scan Usage Description.
Pay attention: the description you provide here will be shown to the user every time your app uses NFC… So make it something useful ;) With that done, you can import the module: simply add
import CoreNFC at the top of your file.
To start reading an NFC tag, you have to create a NFC NDEF reader session and provide a delegate. In the string below,
delegate is called when a tag is found or when an error occurs, and
queue indicates the queue on which the session is executed (if nil, the internal queue is used). Finally,
invalidateAfterFirstRead determines whether you’ll read 1 tag and then terminate automatically (true) or whether you’ll keep reading until the user terminates the session or after a 60-second timeout (false).
let session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true) session.begin()
As a final remark, it’s important to note that the class that you want to use as a delegate needs to comply with the
NFCNDEFReaderSessionDelegate protocol and needs to implement its two methods:
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error)
Is called whenever a read session is terminated with or without an error.
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage])
Is called whenever the reader session finds a new tag. Here, the
messages array contains all the information (format, type, identifier, actual payload) as records. Usually, only one message is provided. Depending on the use-case, you can use the format and type to determine what you can do with the payload.
In the case of Happy Caps, the chip on the vending machine contains a unique ID for that specific machine. When we scan this chip, we retrieve that ID from the record’s payload to trigger an action – for instance, receiving a drink!
Of course, we can think up many more use cases for NFC, and are hoping this will be the starting point for Apple to fully embrace all of the opportunities NFC offers!