Wireless exchange of information between an iPhone and any other connected devices using NFC tags is finally here. There are multiple use cases for 3rd-party apps, but what’s it all about? Let us show you where to start you NFC adventure.
Since NFC (Near Field Communication) was added by Apple to iPhone 6 and 6 Plus, developers had hoped to use this functionality in many applications. They wanted to add NFC tag integration into their favourite apps too, and, for instance, pair devices. They were proposing various creative uses, but Apple had other plans. The company has announced that the NFC chip will be available only through Apple Pay, their new mobile payments platform.
Apple hadn't shared their technology with third-party developers for years, but now the SDK for NFC (CoreNFC) is finally open. But it would be too good if there wasn’t a catch. One of the main drawbacks for the developers is that so far reading NFC tags is supported only on iPhone 7 and iPhone 7 Plus.
From the very beginning software developers were looking forward to the NFC opening up for them and enabling them to access iPhone’s capabilities .This became possible with iOS 11.
Programmers are now able to create apps that can read NFC tags, so in the nearest future we are sure to see many useful and creative applications of this technology.
But first things first.
The framework called “Core NFC” enables apps to read NFC tags compatible with the NFC Data Exchange Format — NDEF. Developers can find framework capabilities in this documentation .
Wondering where to begin and how to set up your project to make use of NFC tags before you get the code? Here’s a short manual to help you get started.
Congrats, it’s almost done! Now it’s time to make your code work.
You need to create an object that will capture information from tags, let’s call it ‘NFCReader.swift’. Then import a framework (CoreNFC) into the object — it will be used during tag scanning.
import CoreNFC
The most important part of NFCReader will be the scanner session object — NFCNDEFReaderSession. Create it in the NFCReader class.
class NFCReader: NSObject { private var session: NFCNDEFReaderSession? }
To make a use of the session, you need to initialise it first and then start scanning. Add a publicly visible function that will be responsible for initiating a session before starting the scan process.
func begin() { session = NFCNDEFReaderSession(delegate: self, queue: DispatchQueue.main, invalidateAfterFirstRead: false) session?.begin() }
As you can see above, when initialising an NFCNDEFReaderSession object type, you need to create a delegate object that will handle the output of a capture session. Therefore, you need to add an extension for the NFCReader class that supports the NFCNDEFReaderSessionDelegate protocol methods you want to use.
extension NFCReader: NFCNDEFReaderSessionDelegate { func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) { // tbc } func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) { // tbc } }
The data from tags captured by the scanner (NFCNDEFMessage type) will appear in the readerSession :didDetectNDEFs: method. You can take the payload from it and then pass it on. To do this, you need to create a protocol that will transmit necessary data outside.
protocol NFCReaderDelegate { func didReceive(payload: String) } class NFCReader: NSObject { var delegate: NFCReaderDelegate? [...] }
Next, add a new protocol support in the: didDetectNDEFs : method. What’s more, you can handle errors that occur in the readerSession:didInvalidateWithError: method.
extension NFCReader: NFCNDEFReaderSessionDelegate { func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) { messages.forEach { message in message.records.forEach { record in if let string = String(data: record.payload, encoding: .ascii) { delegate?.didReceive(payload: string) } } } } func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) { print(error.localizedDescription) } }
The last part is just using the NFCReader class, running the tag scanner under the NFCReaderDelegate protocol:
nfcReader = NFCReader() nfcReader.delegate = self nfcReader.begin()
Now it’s time to run the app and add your intended use cases. Good luck!
Making it possible to use the NFC tags creates a huge opportunity for developers. They are now able to push the technology further and make our lives easier. There are many options to exchange data with a tap.
Only developers coding for iOS 11 are able to create apps using NFC tags so far. It is probably caused by the hardware, not software. In short, previous iPhone models are not able to provide a carrier magnetic field, they are just meant to react to one. This means poor performance when reading tags. Or perhaps, it’s due to strong NFC hardware connection with Apple Pay.
Despite all the possibilities, the scanner is still heavily controlled by the system. For instance, it’s not possible to scan the tags without displaying the system window — you can see this on the screens above.