Digital Advisory Customer Experience Technology
Izabela Franke
Digital Advisory CX Strategy Retail
Jakub Nawrocki
Digital Transformation Retail
Paweł Wasilewski
Values People
Tomek Jurek
Digital Advisory M-commerce
Izabela Franke
Digital Advisory UX research
Jakub Nawrocki

Featured Insights

Explore all insights
Close
Digital Advisory Customer Experience Technology
Izabela Franke
Digital Advisory CX Strategy Retail
Jakub Nawrocki
Digital Transformation Retail
Paweł Wasilewski
Values People
Tomek Jurek
Digital Advisory M-commerce
Izabela Franke
Digital Advisory UX research
Jakub Nawrocki
Explore all insights

Featured Insights

Technology Mobile Development

Here's a Quick Way to Read NFC Tags Using CoreNFC

Here's a Quick Way to Read NFC Tags Using CoreNFC

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.

How can you use NFC in your apps?

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.

Step-by-step process of setting up the project

  1. First, create a new app in Xcode.
  2. Then, create App ID on the Apple Developer site and make sure that “NFC Tag Reading” is supported.
  3. The next step is creating provisioning profile for the app ID and setting it manually in Xcode (uncheck the „Automatically manage signing” option).
  4. Now you can create the *.entitlements file in your Xcode project with the “com.apple.developer.nfc.readersession.formats” key name and array value in which you need to add an “NDEF” object.
  5. Next, go to the build settings and set the *.entitlements filename under the “Code Signing Entitlements” key.
  6. Open your *.plist file and add the “Privacy — NFC Scan Usage Description” text key value (it will appear as a description when trying to scan the tag).

Congrats, it’s almost done! Now it’s time to make your code work.

Here’s how your code will look like.

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!

Wrapping up

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.

Related insights

Arrange consultation with our Digital Advisory & Delivery Team

describe your challenge
Get in touch with our experts to learn more about the benefits of having us by your side.
We engineer
digital business

Subscribe to our insights

A better experience for your customers with Future Mind.

This field is required. Please fill it in, so we can stay in touch
This field is required.
© 2024 Future mind
all rights reserved