npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-nfc-ios

v0.1.5

Published

Use iOS 11+ CoreNFC with React Native

Downloads

25

Readme

Build Status npm version

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

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

<key>com.apple.developer.nfc.readersession.formats</key>
<array>
    <string>NDEF</string>
</array>

Add the NFCReaderUsageDescription key to your project's Info.plist

<key>NFCReaderUsageDescription</key>
<string>Ready to use NFC 🚀</string>

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.

import { EMPTY_RECORD } from 'react-native-nfc-ios';

API

Promise API - One tag at a time

import base64 from 'base-64';
import { NFCNDEFReaderSession } from 'react-native-nfc-ios';

const messages = await NFCNDEFReaderSession.readTag();
const payloadB64 = messages[0].records[0].payload;
const payload = base64.decode(payloadB64);

console.log(payload);
// "React Native NFC iOS"

CoreNFC binding API - Aka Event API

This API is designed to stay as close as possible to CoreNFC.

import { NFCNDEFReaderSession } from 'react-native-nfc-ios';

const readerSession = new NFCNDEFReaderSession();
const listener = readerSession.addEventListener('NDEFMessages', (messages) => {
  console.log(messages);
});

// Show the NFC reader
readerSession.begin();

// Close the NFC reader
readerSession.invalidate();

// Remove the event listener
readerSession.removeEventListener('NDEFMessages', listener);

// Or Remove all events listeners
readerSession.removeAllEventListeners('NDEFMessages');

// ⚠️ Release the native instance to free memory
readerSession.release();

Set the Alert Message

As with the native CoreNFC API you can set the alert message

// With the Simple API
const messages = await NFCNDEFReaderSession.readTag({
  alertMessage: 'Please put your NFC Tag',
});

// As you instantiate a new NFCNDEFReaderSession
const readerSession = new NFCNDEFReaderSession({
  alertMessage: 'Please put your NFC Tag',
});

// Change reader session alert message
readerSession.setAlertMessage('New alert message');