B4i Library NFC - Reading Ndef tags

iOS 11 adds support for reading Ndef tags.
This feature is supported by iPhone 7 and newer devices.


SS-2017-10-08_17.55.13.png


Reading tags is quite simple, though it requires some configuration:

1. Must use an explicit app id (without a wildcard - *).
2. You need to enable NFC Tag Reading in the app id features:

SS-2017-10-08_17.56.42.png


3. Add these attributes:
B4X:
#MinVersion: 11
'privacy description:
#PlistExtra: <key>NFCReaderUsageDescription</key><string>Read nfc tags.</string>
#Entitlement: <key>com.apple.developer.nfc.readersession.formats</key><array><string>NDEF</string><string>TAG</string></array>
4. Make sure to compile with the correct provision profile (the one that references the app id you created).

The code itself is simple. You need to initialize an NFC object. Check the Supported property to make sure that NFC is supported.
Call Scan to start a scan session. The TagDetected event will be raised with the detected messages.
The code in the example parses plain text and URI tags.

Starting from B4i v5.51 the following code should be added:
B4X:
#AdditionalLib: CoreNFC.framework, ReferenceOnly
#DeviceCapabilities: nfc

You must use B4i v5.51+ with NFC.

Updates:
- There was a bug in the example code related to reading of UTF 16 text messages.
 

Attachments

  • iNFC.zip
    15.3 KB · Views: 70
  • NFCExample.zip
    3.3 KB · Views: 56
Last edited:

Duncan H Williamson

Member
Licensed User
Longtime User
iOS 11 adds support for reading Ndef tags.
This feature is supported by iPhone 7 and newer devices.
It requires Xcode 9. For now the hosted builders are running Xcode 8 (they will be upgraded in a month or two) so you must use a local builder.

SS-2017-10-08_17.55.13.png


Reading tags is quite simple, though it requires some configuration:

1. Must use an explicit app id (without a wildcard - *).
2. You need to enable NFC Tag Reading in the app id features:

SS-2017-10-08_17.56.42.png


3. Add these attributes:
B4X:
#MinVersion: 11
'privacy description:
#PlistExtra: <key>NFCReaderUsageDescription</key><string>Read nfc tags.</string>
#Entitlement: <key>com.apple.developer.nfc.readersession.formats</key><array><string>NDEF</string></array>
4. Make sure to compile with the correct provision profile (the one that references the app id you created).

The code itself is simple. You need to initialize an NFC object. Check the Supported property to make sure that NFC is supported.
Call Scan to start a scan session. The TagDetected event will be raised with the detected messages.
The code in the example parses plain text and URI tags.

Hi Erel
is it possible to read the tag UID if the tag has had nothing written to it ?
 

f0raster0

Well-Known Member
Licensed User
Longtime User
hi.. is it posible to send data to the iphone using NFC?
Edit:
I know the new iphone can read NFC only.. I need to read data only.
if yes, I'm going to buy a new iphone
 
Last edited:

f0raster0

Well-Known Member
Licensed User
Longtime User
I mean a device NFC sending a number (example: 34892018920 this number will be changed each time) and the iPhone-App-NFC have to read that number..

This link here has good information but not 100% sure if I can have a device NFC working in card emulated mode to send a number to my future iPhone App.

Note: Erel (or anyone) where is the best place in the forum to ask general questions like this one?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note: Erel (or anyone) where is the best place in the forum to ask general questions like this one?
In a new thread in the questions forum.

NFC tags are passive. They do not send anything. If you emulate a NFC tag with a device (not possible with iOS and probably not possible in Android) then your device can read it.
 

Fusseldieb

Active Member
Licensed User
Longtime User
Hi everybody,
I've pasted this example code into B4i, but unfortunately it says
B4X:
B4i version: 2.50
Parsing code.    Error
Error parsing program.
Error description: Attribute not supported: entitlement
Occurred on line: 14 (Main)
#Entitlement: <key>com.apple.developer.nfc.readersession.formats</key><array><string>NDEF</string></array>
and won't compile at all.
What can this be?
Thanks in advance.
 

Fusseldieb

Active Member
Licensed User
Longtime User
It requires B4i v4.4+
Oh thanks. I'll buy the newest version in a couple of days then. I am starting iOS development and I don't like Xcode in a VM, because it's slow and laggy. B4i for the win :)
 
Top