Skip to content

Commit

Permalink
iOS 13 write, scanNdef, scanTag (#399)
Browse files Browse the repository at this point in the history
* Updates for iOS 13

Implement tag writing on iOS using existing nfc.write function

Add new iOS specific APIs
 * scanNdef
 * scanTag
 * cancelScan

Deprecated old iOS APIs
 * beginSession
 * invalidate

Remove usage of UIWebView and WKWebView. Send event data to Javascript via the channel callback.

Documentation
  • Loading branch information
don authored Apr 16, 2020
1 parent df3afc1 commit a3b1e3a
Show file tree
Hide file tree
Showing 7 changed files with 661 additions and 75 deletions.
11 changes: 11 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
= 1.1.0 =
Implement nfc.write for iOS

This comment has been minimized.

Copy link
@mayartahina

mayartahina Jun 11, 2020

How do we use it?
I am using nfc.write() it fails!!

New iOS specific APIs
* scanNdef
* scanTag
* cancelScan
Deprecated old iOS APIs
* beginSession
* invalidate
Remove usage of UIWebView and WKWebView. Send event data to Javascript via the channel callback.

= 1.0.4 =
Added TAG string for iOS entitlement compatibility #376 Thanks ActionZachson
Add DeviceCapability proximity for Windows 10 builds #358 #265 Thanks DirkHeinke
Expand Down
134 changes: 126 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ See [Getting Started](https://github.com/chariotsolutions/phonegap-nfc/blob/mast

## iOS Notes

Reading NFC NDEF tags is supported on iPhone 7 and iPhone 7 Plus running iOS 11. To enable your app to detect NFC tags, the plugin adds the Near Field Communication Tag Reading capability in your Xcode project. You must build your application with XCode 9. See the [Apple Documentation](http://help.apple.com/xcode/mac/current/#/dev88ff319e7) for more info.
Reading NFC NDEF tags is supported on iPhone 7 (and newer) since iOS 11. iOS 13 added support for writing NDEF messages to NFC tags. iOS 13 also adds the ability to get the UID from some NFC tags. On iOS, the user must start a NFC session to scan for a tag. This is different from Android which can constantly scan for NFC tags. The [nfc.scanNdef](#nfcscanndef) and [nfc.scanTag](#nfcscantag) functions start a NFC scanning session. The NFC tag is returned to the caller via a Promise. If your existing code uses the deprecated [nfc.beginSession](#nfcbeginsession), update it to use `nfc.scanNdef`.

Use [nfc.addNdefListener](#nfcaddndeflistener) to read NDEF NFC tags with iOS. Unfortunately, iOS also requires you to begin a session before scanning NFC tag. The JavaScript API contains two new iOS specific functions [nfc.beginSession](#nfcbeginsession) and [nfc.invalidateSession](#nfcinvalidatesession).
The `scanNdef` function uses [NFCNDEFReaderSession](https://developer.apple.com/documentation/corenfc/nfcndefreadersession) to detect NFC Data Exchange Format (NDEF) tags. `scanTag` uses the newer [NFCTagReaderSession](https://developer.apple.com/documentation/corenfc/nfctagreadersession) available in iOS 13 to detect ISO15693, FeliCa, and MIFARE tags. The `scanTag` function will include the tag UID and tag type for *some* NFC tags along with the NDEF messages. `scanTag` can also read some RFID tags without NDEF messsages. `scanTag` will not scan some NDEF tags including Topaz and Mifare Classic.

You must call [nfc.beginSession](#nfcbeginsession) before every scan.
You must call [nfc.scanNdef](#nfcscanndef) and [nfc.scanTag](#nfcscantag) before every scan.

The initial iOS version plugin does not support scanning multiple tags (invalidateAfterFirstRead:FALSE) or setting the alertMessage. If you have use cases or suggestions on the best way to support multi-read or alert messages, open a ticket for discussion.
Writing NFC tags on iOS uses the same [nfc.write](#nfcwrite) function as other platforms. Although it's the same function, the behavior is different on iOS. Calling `nfc.write` on an iOS device will start a new scanning session and write data to the scanned tag.

# NFC

Expand All @@ -90,8 +90,11 @@ The initial iOS version plugin does not support scanning multiple tags (invalida
- [nfc.stopHandover](#nfcstophandover)
- [nfc.enabled](#nfcenabled)
- [nfc.showSettings](#nfcshowsettings)
- [nfc.beginSession](#nfcbeginsession)
- [nfc.invalidateSession](#nfcinvalidatesession)
- [~~nfc.beginSession~~](#nfcbeginsession)
- [~~nfc.invalidateSession~~](#nfcinvalidatesession)
- [nfc.scanNdef](#nfcscanndef)
- [nfc.scanTag](#nfcscanTag)
- [nfc.cancelScan](#nfccancelscan)

## ReaderMode

Expand Down Expand Up @@ -303,12 +306,14 @@ Function `nfc.write` writes an NdefMessage to a NFC tag.

On **Android** this method *must* be called from within an NDEF Event Handler.
On **Windows** this method *may* be called from within the NDEF Event Handler.
On **iOS** this method should be called outside the NDEF Event Handler, it will starts a new scanning session.

On **Windows Phone 8.1** this method should be called outside the NDEF Event Handler, otherwise Windows tries to read the tag contents as you are writing to the tag.

### Supported Platforms

- Android
- iOS
- Windows
- BlackBerry 7
- Windows Phone 8
Expand Down Expand Up @@ -545,13 +550,17 @@ Windows will return **NO_NFC_OR_NFC_DISABLED** when NFC is not present or disabl

## nfc.beginSession

**`beginSession` is deprecated. Use `scanNdef` or `scanTag`**

iOS requires you to begin a session before scanning a NFC tag.

nfc.beginSession(success, failure);

### Description

Function `beginSession` starts the [NFCNDEFReaderSession](https://developer.apple.com/documentation/corenfc/nfcndefreadersession) allowing iOS to scan NFC tags.
**`beginSession` is deprecated. Use `scanNdef` or `scanTag`**

Function `beginSession` starts the [NFCNDEFReaderSession](https://developer.apple.com/documentation/corenfc/nfcndefreadersession) allowing iOS to scan NFC tags. Use [nfc.addNdefListener](#nfcaddndeflistener) to receive the results of the scan.

### Parameters

Expand All @@ -568,6 +577,8 @@ Function `beginSession` starts the [NFCNDEFReaderSession](https://developer.appl

## nfc.invalidateSession

**`invalidateSession` is deprecated. Use `cancelScan``.**

Invalidate the NFC session.

nfc.invalidateSession(success, failure);
Expand All @@ -589,6 +600,113 @@ Function `invalidateSession` stops the [NFCNDEFReaderSession](https://developer.

- iOS

## nfc.scanNdef

Calling `scanNdef` will being an iOS NFC scanning session. The tag or an error will be returned in a promise.

nfc.scanNdef();

### Description

Function `scanNdef` starts the [NFCNDEFReaderSession](https://developer.apple.com/documentation/corenfc/nfcndefreadersession) allowing iOS to scan NFC tags.

### Returns

- Promise

### Quick Example

// Promise
nfc.scanNdef().then(
tag => console.log(JSON.stringify(tag)),
err => console.log(err)
);

// Async Await
try {
let tag = await nfc.scanNdef();
console.log(JSON.stringify(tag));
} catch (err) {
console.log(err);
}


### Supported Platforms

- iOS

## nfc.scanTag

Calling `scanTag` will being an iOS NFC scanning session. The tag or an error will be returned in a promise.

nfc.scanTag();

### Description

Function `scanNdef` starts the [NFCTagReaderSession](https://developer.apple.com/documentation/corenfc/nfctagreadersession) allowing iOS to scan NFC tags.

The Tag reader will attempt to get the UID from the NFC Tag. If can also read the UID from some non-NDEF tags.

### Returns

- Promise

### Quick Example

// Promise
nfc.scanTag().then(
tag => {
console.log(JSON.stringify(tag))
if (tag.id) {
console.log(nfc.bytesToHexString(tag.id));
}
},
err => console.log(err)
);

// Async Await
try {
let tag = await nfc.scanTag();
console.log(JSON.stringify(tag));
if (tag.id) {
console.log(nfc.bytesToHexString(tag.id));
}
} catch (err) {
console.log(err);
}


### Supported Platforms

- iOS


## nfc.cancelScan

Invalidate the NFC session started by `scanNdef` or `scanTag`.

nfc.cancelScan();

### Description

Function `cancelScan` stops the [NFCReaderSession](https://developer.apple.com/documentation/corenfc/nfcreadersession) returning control to your app.

### Returns

- Promise

### Quick Example

nfc.cancelScan().then(
success => { console.log('Cancelled NFC session')},
err => { console.log(`Error cancelling session ${err}`)}
);

### Supported Platforms

- iOS


# Reader Mode Functions

## nfc.readerMode
Expand Down Expand Up @@ -1146,7 +1264,7 @@ License

The MIT License

Copyright (c) 2011-2017 Chariot Solutions
Copyright (c) 2011-2020 Chariot Solutions

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phonegap-nfc",
"version": "1.0.4",
"version": "1.1.0",
"description": "Near Field Communication (NFC) Plugin. Read and write NDEF messages to NFC tags and share NDEF messages with peers.",
"cordova": {
"id": "phonegap-nfc",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="phonegap-nfc"
version="1.0.4">
version="1.1.0">

<name>NFC</name>

Expand Down
16 changes: 14 additions & 2 deletions src/ios/NfcPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// NfcPlugin.h
// PhoneGap NFC - Cordova Plugin
//
// (c) 2107 Don Coleman
// (c) 2107-2020 Don Coleman

#ifndef NfcPlugin_h
#define NfcPlugin_h
Expand All @@ -11,17 +11,29 @@
#import <CoreNFC/CoreNFC.h>
#import <WebKit/WebKit.h>

@interface NfcPlugin : CDVPlugin <NFCNDEFReaderSessionDelegate> {
@interface NfcPlugin : CDVPlugin <NFCNDEFReaderSessionDelegate, NFCTagReaderSessionDelegate> {
}

// iOS Specific API

// deprecated use scanNdef or scanTag
- (void)beginSession:(CDVInvokedUrlCommand *)command;
// deprecated use stopScan
- (void)invalidateSession:(CDVInvokedUrlCommand *)command;

// Added iOS 13
- (void)scanNdef:(CDVInvokedUrlCommand *)command;
- (void)scanTag:(CDVInvokedUrlCommand *)command;
- (void)cancelScan:(CDVInvokedUrlCommand *)command;

// Standard PhoneGap NFC API
- (void)registerNdef:(CDVInvokedUrlCommand *)command;
- (void)removeNdef:(CDVInvokedUrlCommand *)command;
- (void)enabled:(CDVInvokedUrlCommand *)command;
- (void)writeTag:(CDVInvokedUrlCommand *)command;

// Internal implementation
- (void)channel:(CDVInvokedUrlCommand *)command;

@end

Expand Down
Loading

0 comments on commit a3b1e3a

Please sign in to comment.