CoreNFC for iOS 11: How to make an NFC Reader for your iPhone.

Michał Banaszyński on 1 February 2018

It’s been over 7 years since Google unveiled its first flagship phone with NFC (Near Field Communication) support – the Nexus S. They did this along with the release of Android 2.3, codenamed Gingerbread. From that moment, every iPhone user (shout out to all the tech geeks out there) was waiting for Apple to embed this technology into their operating system. It took them several years to do it. In 2015, they came out with Apple Pay, which wasn’t exactly the same concept as Google’s, but was a step forward in the right direction. At that time, they allowed every owner of an iPhone 6 to pay for their shopping with a phone. For many people, it was a big thing. Unfortunately, its availability was limited to economically developed countries, like e.g. USA, China, UK, Canada, etc. But don’t worry if your country isn’t on the list yet, it grows bigger every year.

Apple Pay uses NFC to emulate its user’s credit card when they try to pay. The payment terminal then communicates with a phone (or Apple Watch nowadays) the same way as it does with a physical payment card. The only thing the user needs to do at the checkout is just to hold the device near a contactless reader and use Touch ID when prompted for authentication. That’s it, so simple. But there was no other way for Apple device owners to experience the NFC technology. Now, the iOS 11 release brought developers a new tool to make their apps more interconnected with the outside world – CoreNFC.

Just like Apple usually does, they wait for a new technology to immerse itself in the marketplace, only then to implement their own solution. For the time being, developers can use the NFC technology just to read a payload sent from a transmitter. There are still many cases of use for this kind of situation like, e.g.:

  • Connecting devices via bluetooth;
  • Marketing (placing tags around the city for people to read);
  • Warehouses (scanning articles on shelves);
  • And many more – “Sky is the limit”.

But wait, what exactly is NFC? How does it work? How can I use it? Let me briefly walk you through the basic concepts of this technology.

Near Field Communication, known simply as NFC, is a wireless technology that allows the exchange of information between devices placed in close proximity. It is actually a collection of a number of different standards and protocols. All of them coexist and are used in different applications, giving the user a flawless experience without the need to switch between anything. The most commonly used application of the NFC technology is NFC Tags. Usually represented as little stickers or cards which can be read by an NFC Readers.

source: androidauthority.net

NFC Tags are passive devices – it means that they do not need any traditional power supply to work. They are triggered by a device present in a supported range. In order to power up the tag, electromagnetic induction needs to be created to produce a current used to transfer data into an active device (a phone in this case). That is why your phone needs to be within close range of the tag for this to work.

Fair enough, let’s skip to the main reason why we’re here. How can you build an NFC iPhone Reader? Get your notepad ready and roll up your sleeves. Let’s dive into it!

source: https://gph.is/1LjlEFE

Our app will display all of the NFC messages in a UITableViewController. For the purpose of this article, we’ve prepared a complete Xcode project; just visit our GitHub page at GITHUB URL and compile by yourself.

Example Xcode project app preview

There are a few things we need to do before we type the first line of code. To support NFC, the project needs a few tweaks. CoreNFC is an entitlement-protective framework, so the first thing to do is turn on the Near Field Communication Tag Reading feature inside your project’s Capabilities section.

The next step to add a “Privacy — NFC Scan Usage Description” text key value (the text your app will present to a user when using NFC) in your app’s info.plist file.

Note that in Xcode, the simulator does not support NFC at all. You have to use real devices to test your app’s behaviour. Secondly, your application needs to be foregrounded in order to start the tag reading activity. The reading session terminates when the application is going to be backgrounded or after the 60second time frame expires.

Now we are good to go and finally start creating things. The process of reading an NFC tag can be started by creating the session – NFCNDEFReaderSession. This is a class responsible for maintaining NFC tag reading sessions. There can only be one session active in a system at a time. If you create an additional one, the system puts it in a queue and processes it in a first-in, first-out (FIFO) order.

To create a session, we need to import the CoreNFC framework to our sourcefile:

Then, we have to make sure the NFC Session object will not be retained, so a strong reference needs to be created:

Every time Scan button is being pressed we need to create an instance of it and start a session let’s put that piece of code in our scanButtonAction(_ sender:) method:

Let’s break down the NFCNDEFReaderSession initializer:

  1. delegateNFCNDEFReaderSessionDelegate a protocol conforming object which handles session possible callbacks.
  2. queue – a queue to which delegate callbacks are dispatched, in case of nil, an internal serial dispatch queue is created for the session.
  3. invalidateAfterFirstRead – a value indicating whether or not the session is going to be invalidated after the first tag read out.

For a single tag session configuration, with every successful readout, the delegate’s readerSession(_:didDetectNDEFs:) method delivers the message, then terminates. If your application reads multiple tags in a single session, to stop the delegate from getting callbacks, simply call invalidate().

So let’s make our ViewController conform to NFCNDEFReaderSessionDelegate protocol:

That’s it. We’ve just created a fully working NFC reader. How simple was that?! There are just a few things we have to take care of, which makes this new framework so easy to use. Just keep the session alive and handle delegate callbacks; this is the place your application lets you know about an NFC supported device the iPhone is interacting with. It can be your product like speakers, headphones, etc. The list of use cases for this kind of technology is endless. We hope you’ll find one, let us know in the comment section down below this article!

In case you have any questions, feel free to leave a comment as well, we will be more than happy to help you with your iOS journey. Stay tuned for more “What’s new in iOS 11” articles like this one!

Check our portfolio - Appchance software house

 

Tagi: , , , , , , , , ,

You might also like

2 thoughts on “CoreNFC for iOS 11: How to make an NFC Reader for your iPhone.”

  1. Is there a way I that we can exchange device information such as os version and what type of device via nfc tag?

  2. If this will be included in the payload sent via NFC you can provide such information as well 🙂 Hopefully this fulfills your requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Appchance web and mobile app services
Check how to flourish
your business using
mobile app
I have read and accepted the terms of use.