comments 7

#53 First steps with NFC on iOS 13

In this post I would like to point the most important things which were mentioned during Core NFC Enhancements talk on WWDC 2019. Right now I'm starting the new project where NFC knowledge will be very useful for me. Writing this post gave me a kick to learn more about the news regarding NFC from the last Apple conference.

I hope this post will help you to start to use the new NFC features. If you have some comments or you could share some of your experience with NFC, please leave a comment!

Basics

Before we start to write some code let's talk briefly about NFC itself.

What is NFC?

NFC stands for Near Field Communication. NFC is a set of standards that allow smartphones and other devices to communicate via radio signals when they are held in close proximity.

Near-field communication devices operate at the same frequency (13.56 MHz) as HF RFID (high frequency Radio-Frequency identification) readers and tags. The standards and protocols of the NFC format is based on RFID standards outlined in ISO/IEC 14443, FeliCa, and the basis for parts of ISO/IEC 18092.

NFC is primarily being used for wireless payments with services like Google Pay, Apple Pay, and Samsung Pay and in many other areas like marketing, public transport, helthcare, etc.

Payments logo

What is NDEF?

The NFC Data Exchange Format (NDEF) is a standardized data format that can be used to exchange information between any compatible NFC device and another NFC device or tag. The NDEF format is used to store and exchange information like URIs, plain text, etc.

The news from WWDC

In iOS 13 Apple expanded possibilities of Core NFC. Now iOS is able to read more complex NFC tags, including those that meet ISO 7816 and ISO 15693 standards. Now is enabled to read the data stored on tags associated with passports, driving licences, and other forms of official identification.

The second important thing is that now iPhone apps will be able to write directly to blank tags.

What's new in the software

What's new in the hardware

iPhone XS, XS Max and XR (with the new A12 Bionic chip) posses a background feature that allows them to scan NFC tags without the need to open an app.

Before you start

Let's create a project in Xcode for an app that reads/writes NFC tags and:

1. Add NFC capabilities to your project

capabilities

2. Provide a non-empty string for the NFCReaderUsageDescription key in your app’s info.plist file.

InfoPlist

NDEF Tag writing

Implementation consists of 6 steps:

1. Create session with a NFCNDEFReaderSessionDelegate object

2. Implement optional readerSession(_:didDetect:) method

3. Connect to the NDEF tag

4. Query NDEF status

5. Write NDEF message (with usage of NFCNDEFMessage class)

In our case we used plain text which will be written on tag. This is done by class function wellKnowTypeTextPayload from NFCNDEFPayload class.

6. Invalidate session on completion

Full project you can find here: https://github.com/swiftingio/NFCWriter

Native tag reading

ISO7816

About

ISO/IEC 7816 is an international standard related to electronic identification cards with contacts, especially smart cards. The ISO7816 regulates how the data in Smart Cards should be structured. This protocol is used in passports and id documents.

Requirements
  • Application Info.plist must contain list of application identifiers (AIDs)
  • Detected tags callback is invoked if tag is ISO7816 compliant and contains an AID found in the Info.plist (Photo)
  • Include the Near Field Communication Tag Reader Session Formats Entitlement in your app.
  • Provide a non-empty string for the NFCReaderUsageDescription key in your app’s info.plist file.
NFCISO7816Tag

Properties:

  • identifier (UID)
  • historicalBytes
  • applicationData

Methods:

Example

Note: iso14443 Support both Type A & B modulation. NFCTagTypeISO7816Compatible and NFCTagTypeMiFare tags will be discovered.

1. Create session with a NFCTagReaderSessionDelegate object

2. Implement tagReaderSession(_:didDetect:) method

3. Connect to the ISO7816 tag

4. Send APDU and receive response NFCISO7816APDU

APDU – Application protocol data unit is the communication unit between a reader and a nfc tag. More about APDU message you can find here. Some example instructions (based on https://www.nxp.com/docs/en/data-sheet/MF3ICDX214181_SDS.pdf):

  • INS code ‘A4’ SELECT
  • INS code ‘B0’ READ BINARY
  • INS code ‘D6’ UPDATE BINARY
  • INS code ‘B2’ READ RECORDS
  • INS code ‘E2’ APPEND RECORD
  • INS code ‘84’ GET CHALLENGE
  • INS code ‘88’ INTERNAL AUTHENTICATE
  • INS code ‘82’ EXTERNAL AUTHENTICATE

5. Terminate session with error (optional)

MIFARE

About

MIFARE logo

The NXP Semiconductors-owned trademark of a series of chips used in contactless smart cards and proximity cards. Typical read/write distance of up to 10 cm (4 inches). Used for example in public transport, access cards, hospitality, loyalty, and micropayment services.

NFCMiFareTag

Properties:

  • identifier (UID)
  • historicalBytes
  • mifareFamily — Ultralight , Plus, DESFire

Methods:

Example

1. Create session with an NFCTagReaderSessionDelegate object

2. Implement tagReaderSession(_:didDetect:) method

ISO15693

About

ISO15693 is an ISO standard for vicinity cards, i.e. cards which can be read from a greater distance as compared with proximity cards. Example of usage:

  • in libraries for tracking books
  • in ski passes
NFCISO15693Tag

Properties:

  • identifier (UID)
  • icManufacturerCode
  • icSerialNumber

Methods:

  • Read single block
  • Read multiple blocks
  • Extended read
  • Lock block and etc. (all methods you can find in the documentation)
Example

1. Create session with a NFCTagReaderSessionDelegate object

2. Implement tagReaderSession(_:didDetect:) method

FeliCa

About

Felica logo

FeliCa is a contactless smart card system from Sony in Japan. The name stands for Felicity Card. A proximity of 10 centimeters or less is required for communication. FeliCa is used in a wide variety of ways, such as in ticketing systems for public transportations, e-money, and residence door keys.

Requirements
  • Application Info.plist must contain list of system codes (Detected tags callback is invoked if tag contains a system code found in the Info.plist)
NFCFeliCaTag

Properties:

  • currentSystemCode
  • currentIDM

Methods:

Example

1. Create session with a NFCTagReaderSessionDelegate object

2. Implement tagReaderSession(_:didDetect:) method

Summary

I hope this article was helpful to you to start your journey with NFC. Meanwhile, when I will have more experience with the current project I would like to share some more advanced code snippets. Don't forget to leave some comment in the case of any insight from your side!

External sources

7 Comments

  1. Ryan

    So…although the changes mean that select devices can write directly to tag, would this theoretically mean that a device itself could also emit a NFCNDEFMessage as a readable signal to another device?

    For example, the device itself could act as “the tag”, in a way. And another device, when in proximity, could detect, retrieve, and read the NDEF message from the device of origin?

  2. rldaulton@gmail.com

    So…although the changes mean that select devices can write directly to tag, would this theoretically mean that a device itself could also emit a NFCNDEFMessage as a readable signal to another device?

    For example, the device itself could act as “the tag”, in a way. And another device, when in proximity, could detect, retrieve, and read the NDEF message from the device of origin?

  3. Richie

    Hey – when I try this with a NTAG215, it fails in the tag.writeNDEF(myMessage) completion with a Tag is not connected error – any ideas why this might be? (on ios13 beta 4)

  4. Mark

    What “standard” did you settle on for your exploration? I’d like to buy some NFC badges to read and write but there’s a ton out there and I’m assuming some are easier than others.

  5. Luciano Nascimento

    Amazing this post! It help me a lot, because I’m developing a app for a company of transport in my city, and just iPhone we are with problem to implementing, but now I will be able to finish them. Thank you so much!

Leave a Reply


*