React Native NFC for iOS
⚠️ Apple CoreNFC is only available for iOS11 on iPhone 7 and iPhone 7 Plus devices. It does not seems to be available on simulator at the moment, but it should be available later.
Demo
How to use
Installation
Install the module
npm install --save react-native-nfc-ios
Link the native module to your project
react-native link react-native-nfc-ios
Prepare your Xcode project
Add the NFC capability key to your .entitlements
file
com.apple.developer.nfc.readersession.formats NDEF
Add the NFCReaderUsageDescription
key to your project's Info.plist
NFCReaderUsageDescriptionReady to use NFC 🚀
Lean about NDEF Messages Structure (NFC Data Exchange Format)
As CoreNFC, the API will return arrays of messages, each message containing an array of records.
"records": "type": "VQ==" // base64 encoded for 55, URI record "payload": "UmVhY3QgTmF0aXZlIE5GQyBpT1M=" // base64 encoded for "React Native NFC iOS" "identifier": null // No identifier in the tag "typeNameFormat": "WELL_KNOWN_RECORD"
Every record will have a Base64 encoded type
, payload
and identifier
.
The typeFormatName
will be one of the following constant :
EMPTY_RECORD
WELL_KNOWN_RECORD
MIME_MEDIA_RECORD
ABSOLUTE_URI_RECORD
EXTERNAL_RECORD
UNKNOWN_RECORD
UNCHANGED_RECORD
You can import those constants form the module.
;
API
Promise API - One tag at a time
;; const messages = await NFCNDEFReaderSession;const payloadB64 = messages0records0payload;const payload = base64; console;// "React Native NFC iOS"
CoreNFC binding API - Aka Event API
This API is designed to stay as close as possible to CoreNFC.
; const readerSession = ;const listener = readerSession; // Show the NFC readerreaderSession; // Close the NFC readerreaderSession; // Remove the event listenerreaderSession; // Or Remove all events listenersreaderSession; // ⚠️ Release the native instance to free memoryreaderSession;
Set the Alert Message
As with the native CoreNFC API you can set the alert message
// With the Simple APIconst messages = await NFCNDEFReaderSession; // As you instantiate a new NFCNDEFReaderSessionconst readerSession = alertMessage: 'Please put your NFC Tag'; // Change reader session alert messagereaderSession;