Skip to content

Commit

Permalink
doc: enhance readme
Browse files Browse the repository at this point in the history
  • Loading branch information
whitedogg13 committed Feb 18, 2021
1 parent 8b45c91 commit 9068abf
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 52 deletions.
12 changes: 12 additions & 0 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ The same as above, please check if the tag is properly formatted, and contain at
## [iOS] cannot read / write Mifare Classic

Indeed, currently MifareClassic isn't supported by Core NFC in our tests. It is also not listed in Core NFC's [NFCMiFareFamily](https://developer.apple.com/documentation/corenfc/nfcmifarefamily?language=objc)

## [Android] My NFC tag cannot launch my app

Note on getLaunchTagEvent: keep in mind that you can only create intent-filters for the very first NDEF record on an NFC tag! If your intent-filter doesn't match the FIRST record your app will launch but it won't get the tag data. Check out for details:
https://stackoverflow.com/questions/25504418/get-nfc-tag-with-ndef-android-application-record-aar/25510642

Also you should add
```xml
android:launchMode="singleTask"
```
to your manifest to prevent launching your app as another task when it is already running.

153 changes: 101 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,97 +8,146 @@ Bring NFC feature to React Native. Inspired by [phonegap-nfc](https://github.com

Contributions are welcome!

## iOS 14 Simulator issue

As mentioned in [issue 326](https://github.com/whitedogg13/react-native-nfc-manager/issues/326), iOS 14 simulator will crash when using CoreNFC, however real iOS 14 device works.

## Install

```shell
# RN >= 0.60
npm i --save react-native-nfc-manager
```
### javascript part

```shell
# RN < 0.60 (without the latest iOS 13 feature)
npm i --save [email protected]
npm i --save react-native-nfc-manager
```

## Setup
### native part

This library use native-modules, so you will need to do `pod install` for iOS:

```shell
# RN >= 0.60, iOS
cd ios && pod install && cd ..
# ...then open ios/xxx.xcworkspace...
```

```shell
# RN >= 0.60, Android
# This module leverages autolink, so no extra steps are required
```
(see [here](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md#autolinking) for more info about autolink)
For Android, it should be properly auto-linked, so you don't need to do anything.

## Setup

```shell
# RN < 0.60, both platforms
react-native link react-native-nfc-manager
```
Please see [here](setup.md)

## Extra iOS setup is required
## Demo

You will need to setup some capabilities / entitlement / plist stuff to enable NFC development on your device, this repo explains these requirements very well:
Please see this project: [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)

* https://github.com/hansemannn/iOS11-NFC-Example
## Basic Usage

**IMPORTANT: For the new NFC capabilities available on iOS 13 to work, the entitlements file mentioned in the previous guide should look like this:**
If all you want to do is to read `NDEF` data, you can use this example:

```xml
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
<string>NDEF</string>
<string>TAG</string>
</array>
```
```javascript
import NfcManager, {NfcEvents} from 'react-native-nfc-manager';

In the ISO18092 system codes for NFC Tag Reader Session, all FeliCa system codes used for reading must be entered in advance. Wildcards cannot be used.
function readNdef() {
const cleanUp = () => {
NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
NfcManager.setEventListener(NfcEvents.SessionClosed, null);
};

## Launch app on nfc event
return new Promise((resolve) => {
let tagFound = null;

Note on getLaunchTagEvent: keep in mind that you can only create intent-filters for the very first NDEF record on an NFC tag! If your intent-filter doesn't match the FIRST record your app will launch but it won't get the tag data. Check out for details:
https://stackoverflow.com/questions/25504418/get-nfc-tag-with-ndef-android-application-record-aar/25510642
NfcManager.setEventListener(NfcEvents.DiscoverTag, (tag) => {
tagFound = tag;
resolve(tagFound);
NfcManager.setAlertMessageIOS('NDEF tag found');
NfcManager.unregisterTagEvent().catch(() => 0);
});

Also you should add
```xml
android:launchMode="singleTask"
NfcManager.setEventListener(NfcEvents.SessionClosed, () => {
cleanUp();
if (!tagFound) {
resolve();
}
});

NfcManager.registerTagEvent();
});
}
```

Anything else, ex: write NDEF, send custom command, please read next section.

## Advanced Usage

In high level, there're 3 steps to perform advanced NFC operations:
1. request your specific NFC technology
2. select the proper NFC technology handler, which is implemented as getter in main `NfcManager` object, including:
* ndefHandler
* nfcAHandler
* isoDepHandler
* iso15693HandlerIOS
* mifareClassicHandlerAndroid
* mifareUltralightHandlerAndroid
3. call specific methods on the NFC technology handler
4. clean up your tech registration

For example, here's an example to write NDEF:

```javascript
import NfcManager, {NfcTech, Ndef} from 'react-native-nfc-manager';

async function writeNdef({type, value}) {
let result = false;

try {
// Step 1
await NfcManager.requestTechnology(NfcTech.Ndef, {
alertMessage: 'Ready to write some NDEF',
});

const bytes = Ndef.encodeMessage([Ndef.textRecord("Hello NFC")]);

if (bytes) {
await NfcManager
.ndefHandler // Step2
.writeNdefMessage(bytes); // Step3

if (Platform.OS === 'ios') {
await NfcManager.setAlertMessageIOS('Successfully write NDEF');
}
}

result = true;
} catch (ex) {
console.warn(ex);
}

// Step 4
NfcManager.cancelTechnologyRequest().catch(() => 0);
return result;
}
```
to your manifest to prevent launching your app as another task when it is already running.

## Demo project
To see more examples, please see [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)

Please use [this repo](https://github.com/whitedogg13/nfc-test-app) as a quick start.
## API

## Example codes
Please see [here](index.d.ts)

Look into `example` for the features you need.
## FAQ

**v2 examples**
Please see [here](FAQ.md)

## Legacy (v1, v2) docs

### v2

* [v2 doc](APIv2.md)
* [v2-ios+android-read-ndef](example/AppV2.js)
* [v2-ios+android-write-ndef](example/AppV2Ndef.js)
* [v2-ios+android-get-uid](example/AppV2Mifare.js)
* [v2-ios+android-mifare-custom-command](example/AppV2Mifare.js)

**v1 examples**
### v1

* [v1 doc](APIv1.md)
* [v1-ios-read-ndef](example/App.js)
* [v1-android-read-write-ndef](example/App.js)
* [v1-android-mifare-classic](example/AndroidMifareClassic.js)
* [v1-android-read-write-ndef-with-ndef-tech](example/AndroidTechTestNdef.js)

## API Document

* [v2 doc](APIv2.md)
* [v1 doc](APIv1.md)


Binary file added images/edit-entitlement.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/enable-capability.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## iOS

1. In [apple developer site](https://developer.apple.com/), enable capability for NFC

![enable capability](./images/enable-capability.png "enable capability")

2. in Xcode, add `NFCReaderUsageDescription` into your `info.plist`, for example:

```
<key>NFCReaderUsageDescription</key>
<string>We need to use NFC</string>
```

More info on Apple's [doc](https://developer.apple.com/documentation/bundleresources/information_property_list/nfcreaderusagedescription?language=objc)

3. (optional, if using specific NFC technology) in Xcode, edit entitlement for your need, for example:

![edit entitlement](./images/edit-entitlement.png "edit entitlement")

More info on Apple's [doc](https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_nfc_readersession_formats?language=objc)

## Android

Simple add `uses-permission` into your `AndroidManifest.xml`:

```xml
<uses-permission android:name="android.permission.NFC" />
```

0 comments on commit 9068abf

Please sign in to comment.