How to integrate Core NFC in iOS

NFC is the technology that permits two devices—like your phone or tablet and a payments terminal—to talk to each other when they’re close together. NFC is the technology that allows contactless payments.

What is Core NFC?

Technically by Core NFC, you can read Near Field Communication (NFC) tags of types 1 through 5 that cover data in the NFC Data Exchange Format (NDEF). Your app creates an NFC NDEF reader session and provides a delegate, for reading a tag. When it finds tags that have NDEF messages, a running reader session polls for NFC tags and calls the delegate, passing the messages to the delegate. The delegate can read the messages and handle situations that can cause a session to become invalid.

How does NFC work?

NFC, is a wireless technology that permits the exchange of information between devices placed in close proximity over radio waves. NFC can be used to just send data which is faster than Bluetooth as well as to induce electric currents within passive components. 13.56 MHz is frequency of NFC’s data-transmission and can transmit data at either 106, 212 or 424 kilobits per second.Currently NFC standard has 3 modes of operation: a read/write mode in which one active device picks information from a passive one, card emulation, in which an NFC device can be used like a contactless credit card and the peer-to-peer mode that lets smartphones swap data.

Uses of NFC:

  1. Marketing (setting tags everywhere in the city for people to read)
  2. Warehouses (scanning articles on items)
  3. Mobile Payments
  4. Business Cards
  5. Ticketing

Applications:

  1. NFC Actions
  2. GoToTags
  3. NFC Reader & Scanner

Requirements:

  1. iOS 11
  2. XCode 9
  3. Swift 4
  4. iPhone7/iPhone7 plus or above.

Mentioned are the requisites for implementing NFC in iPhone app development.

Integration in XCode:

There are a few things we need to do before we type the first line of code. The project needs a few tweaks to support NFC.The first thing to do is turn on the Near Field Communication Tag Reading feature inside your project’s Capabilities section as CoreNFC is an entitlement-protective framework. The next is 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.

The simulator does not support NFC at all. You have to use real devices to test your app’s behavior.

First of all we need to import coreNFC module

import CoreNFC

Add this property to your view controller:

var session: NFCNDEFReaderSession?

Now, go to viewDidLoad()  and add these two lines of code:

session = NFCNDEFReaderSession(delegate: self, queue: DispatchQueue.main, invalidateAfterFirstRead: false)  session?.begin()

ViewController class doesn’t currently conform to the NFCNDEFReaderSessionDelegate protocol, so you’ll need to amend your class definition to include it

class ViewController: UIViewController, NFCNDEFReaderSessionDelegate
	{

NFCNDEFReaderSessionDelegate Methods:

func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: ErrorMessage)
	 
	{
	 
	//Error handling code here }
	 
	func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: ErrorMessage) {      print(error.localizedDescription)  // print the error description   
	}

Now for the didDetectNDEFs method. When this method is called, you’ll get an array of detected messages, each message can contain one or more records describing a single piece of data

A single NFCNDEFPayload contains 4 items of information:
1. Identifier, 2.Type, 3.TypeNameFormat and 4. Payload which is an object of type Data

func readerSession (_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
	 
	for message in messages {
	 
	for record in message.records {
	 
	if let string = String(data: record.payload, encoding: .ascii) {                    
	print(string)    //print string
	 
	}
	 
	}
	 
	}
	 
	}

Take a look at the advantages of using NFC (Near Field Communication) –

  • User Experience many benefits for enterprises for hassle-free payment methods. Easy UX increases the engagement, productivity and customer retention due to the comfort zone they found.
  • Security is kept as the priority as NFC is used with the information that details with mobile wallets or the personalized information of physical credit or debit cards for quick swipe payments.
  • Versatile as NFC usage is compatible for various domains and their services. Right from banking to hospitality industry, each can have their various purpose of using NFC based application. Rewards, point redemption, coupons and much more are the additional benefits.
  • Convenience of receiving the information. Whether it is a business card or the mobile wallet based payments – with NFC it is well comfortable to just tap or touch and fetch the desired information.
Next article

...

Comments

  • Leave a message...