Skip to content

Commit

Permalink
#11 setOnNdefDiscoveredListener callback is not invoked on main threa…
Browse files Browse the repository at this point in the history
…d in iOS
  • Loading branch information
EddyVerbruggen committed Dec 8, 2017
1 parent 3bf55d3 commit 1b203bd
Show file tree
Hide file tree
Showing 14 changed files with 7,324 additions and 145 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ tns plugin add nativescript-nfc
## iOS Setup
iOS requires you to enable 'NFC Tag Reading' for your App ID [here](https://developer.apple.com/account/ios/identifier/bundle).

Note that to be able to use NFC you'll need to build with Xcode 9 (beta), so open Xcode and run the app from there.
Note that to be able to use NFC you'll need to have Xcode 9 installed, and (at least) NativeScript version 3.2.0 (so if `tns --version` is lower, do `npm i -g nativescript`).

Also, add this to your `App_Resources/iOS/*.entitlements` file:
Also, add this to your `App_Resources/iOS/app.entitlements` (mind the name!) file:

```xml
<key>com.apple.developer.nfc.readersession.formats</key>
Expand Down Expand Up @@ -121,13 +121,16 @@ nfc.enabled().then((on) => {
```

### `setOnNdefDiscoveredListener`
You may want to get notified when an Ndef tag was discovered.
You can pass in a callback function that gets invoked when that is the case.
You may want to get notified when an Ndef tag was discovered. You can pass in a callback function that gets invoked when that is the case.

Note that blank/erased NFC tags are not returned here, but through `setOnTagDiscoveredListener` instead.

See the [definition of NfcNdefData](https://github.com/EddyVerbruggen/nativescript-nfc/blob/master/nfc.common.d.ts#L27-L33) to learn what is returned to the callback function.

For iOS you can pass in these options (see the TypeScript example below):
* `stopAfterFirstRead: boolean` (default `false`): don't continue scanning after a tag was read.
* `scanHint: string` (default `undefined`): Show a little hint in the scan UI.

##### JavaScript
```js
nfc.setOnNdefDiscoveredListener(function(data) {
Expand All @@ -151,6 +154,10 @@ nfc.setOnNdefDiscoveredListener((data: NfcNdefData) => {
console.log("Ndef discovered! Message record: " + record.payloadAsString);
}
}
}, {
// iOS-specific options
stopAfterFirstRead: true,
scanHint: "Scan a tag, baby!"
}).then(() => {
console.log("OnNdefDiscovered listener added");
});
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions demo/app/App_Resources/iOS/build.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;

DEVELOPMENT_TEAM = 8Q5F6M3TNS

CODE_SIGN_ENTITLEMENTS = demo.entitlements
DEVELOPMENT_TEAM = 8Q5F6M3TNS
46 changes: 18 additions & 28 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { alert } from "tns-core-modules/ui/dialogs";
import { Nfc, NfcTagData, NfcNdefData } from "nativescript-nfc";

export class HelloWorldModel extends observable.Observable {
public lastNdefDiscovered: string = "";
public lastNdefDiscovered: string = "Press a button...";
private nfc: Nfc;

constructor() {
Expand Down Expand Up @@ -37,50 +37,40 @@ export class HelloWorldModel extends observable.Observable {
}).then(() => {
console.log("OnTagDiscovered Listener set");
}, (err) => {
alert(err);
console.log(err);
});
}

public doStopTagListener() {
this.nfc.setOnTagDiscoveredListener(null).then(() => {
console.log("OnTagDiscovered nulled");
}, (err) => {
alert(err);
console.log(err);
});
}

public doStartNdefListener() {
const that = this;
this.nfc.setOnNdefDiscoveredListener((data: NfcNdefData) => {
if (data.message) {
let tagMessages = [];
// data.message is an array of records, so:
data.message.forEach(record => {
console.log(">>> record.tnf: " + record.tnf);
console.log(">>> record.type: " + record.type);
console.log(">>> record.payload: " + record.payload);
console.log(">>> record.payloadAsString: " + record.payloadAsString);
console.log(">>> record.payloadAsHexString: " + record.payloadAsHexString);
console.log("Read record: " + JSON.stringify(record));
tagMessages.push(record.payloadAsString);
});
that.set("lastNdefDiscovered", "Read: " + tagMessages.join(", "));
console.log("Read: " + tagMessages.join(", "));
alert({
title: "Ndef tag contents read:",
message: " - " + tagMessages.join("\n - "),
okButtonText: "OK :)"
});
this.set("lastNdefDiscovered", "Read: " + tagMessages.join(", "));
}
}, {stopAfterFirstRead: true}).then(() => {
console.log("OnNdefDiscoveredListener set");
}, (err) => {
alert(err);
});
}, {
stopAfterFirstRead: true,
scanHint: "Scan a tag, baby!"
})
.then(() => this.set("lastNdefDiscovered", "Listening..."))
.catch(err => alert(err));
}

public doStopNdefListener() {
this.nfc.setOnNdefDiscoveredListener(null).then(() => {
console.log("OnNdefDiscoveredListener nulled");
this.set("lastNdefDiscovered", "Stopped listening.");
}, (err) => {
alert(err);
});
Expand All @@ -95,9 +85,9 @@ export class HelloWorldModel extends observable.Observable {
}
]
}).then(() => {
console.log("Wrote text 'Hello!'");
this.set("lastNdefDiscovered", "Wrote text 'Hello!'");
}, (err) => {
alert(err);
console.log(err);
});
}

Expand All @@ -110,17 +100,17 @@ export class HelloWorldModel extends observable.Observable {
}
]
}).then(() => {
console.log("Wrote uri 'https://www.telerik.com");
this.set("lastNdefDiscovered", "Wrote uri 'https://www.telerik.com");
}, (err) => {
alert(err);
console.log(err);
});
}

public doEraseTag() {
this.nfc.eraseTag().then(() => {
console.log("Tag erased");
this.set("lastNdefDiscovered", "Tag erased");
}, (err) => {
alert(err);
console.log(err);
});
}
}
Loading

0 comments on commit 1b203bd

Please sign in to comment.