I just installed the first iOS 11 beta to an iPhone 7 and am interested in trying the NFC. There's nothing about it in settings. I am wondering if there's any sample code out there showing how to read a tag. Can anyone show how to use the Core NFC SDK, in a code snippet?
If you need details just to be sure, here's the whole list of iPhones that are NFC-enabled: iPhone 13, iPhone 13 Mini, iPhone 13 Pro, iPhone 13 Pro Max. iPhone 12, iPhone 12 Mini, iPhone 12 Pro, iPhone 12 Pro Max. iPhone 11, iPhone 11 Pro, iPhone 11 Pro Max, iPhone SE (2nd Gen)
If the icon is not visible on your screen and to add it as a shortcut: Open Settings Select Control Center Scroll and tap the green plus button on the left of NFC Tag Reader (iPhone Tricks, n.d.)
iOS 11 includes an NFC SDK for iOS, Core NFC which allows for iPhone apps to read NDEF records from NFC tags.
In the Apple Developer site, create a new App ID and make sure that NFC Tag Reading
is enabled.
Add the following lines to your .plist file:
<key>NFCReaderUsageDescription</key> <string>NFC Tag!</string>
and these to the entitlements file:
<key>com.apple.developer.nfc.readersession.formats</key> <array> <string>NDEF</string> </array>
It should look something like this in the corresponding files:
Also Core NFC can be enabled via the Capabilities tab in Xcode.
Import CoreNFC
#import <CoreNFC/CoreNFC.h>
and set the delegate:
@interface YourViewController : UIViewController <NFCNDEFReaderSessionDelegate>
In viewDidLoad:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NFCNDEFReaderSession *session = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT) invalidateAfterFirstRead:NO]; [session beginSession]; }
In the delegate callback:
- (void) readerSession:(nonnull NFCNDEFReaderSession *)session didDetectNDEFs:(nonnull NSArray<NFCNDEFMessage *> *)messages { for (NFCNDEFMessage *message in messages) { for (NFCNDEFPayload *payload in message.records) { NSLog(@"Payload data:%@",payload.payload); } } }
You must also add the didInvalidateWithError
delegate callback or you'll not conform with protocol:
- (void)readerSession:(nonnull NFCNDEFReaderSession *)session didInvalidateWithError:(nonnull NSError *)error { }
You can stop the reader with:
[session invalidateSession];
Import CoreNFC
import CoreNFC
and set the delegate:
class YourViewController: UIViewController, NFCNDEFReaderSessionDelegate
In viewDidLoad:
override func viewDidLoad() { super.viewDidLoad() let session = NFCNDEFReaderSession(delegate: self, queue: DispatchQueue(label: "queueName", attributes: .concurrent), invalidateAfterFirstRead: false) session?.begin() }
In the delegate callback:
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) { for message in messages { for record in message.records { print(record.payload) } } }
You can stop the reader with:
session.invalidateSession
After launching the view you should immediately see the iOS NFC reader dialog like so:
Once this dialog appears you have about a second to place the iPhone near the NFC tag you want to read. Otherwise, the field is deactivated (this seems to be a bug on Apple's end). I often needed to cancel and retry to get consistent readings. More details here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With