Group Group Group Group Group Group Group Group Group

Getting Started with Core NFC | raywenderlich.com

In this tutorial, you’ll learn how to use CoreNFC to connect wirelessly to other devices or NFC tags.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/9582458-getting-started-with-core-nfc
1 Like

This for sure is the most comprehensive tutorial on the internet for reading and writing NFC tags so thank you. I have just a couple of questions, I just started learning swift as my first coding language just a month ago so forgive me for any naivety.

  1. I’m looking to assign different objects of the same class to different tags to be read by a user. This class contains different data types such as strings, Ints, and Images. Which payload type would this fall under?

  2. Im trying to write these tags without giving users the option to write, only read and perform an action once read. How would you go about this?

Hey @mjheitz, I’m glad you liked it! You shouldn’t have to worry about sounding naive here, even if you were a seasoned Swift developer - we’re all always learning together.

I’ll do my best to answer your questions:

  1. You should be able to mimic the same thing going on in the tutorial for this. I used Location as a very simple model to write to the devices, but you can replace the references to this with your own Codable type (the protocol your type conforms to to make encoding/decoding easier). It still needs to be saved to the tag using NFCNDEFPayload. When you read the tag, you’ll just attempt to decode the records. Note that an NFC tag can save multiple records in the payload, and this tutorial assumed you would only ever have one value in the payload array.

  2. Writing to a tag requires you to connect to a tag and call writeNDEF(_:). You can just leave out the steps it takes to get to this point in your client code. You’ll need to have some way to initially write your data, and this tutorial attempts to break out that concept with the “Admin” tab. It also writes to the tag when the user “updates” the location, but you can also ignore that for your use case.

To perform an action when a tag is read, you can take a look at readLocation(from:) in the sample code. This calls readNDEF(_:) on the tag. Once a tag is successfully read, you can swap out this code to do whatever you need.

Let me know if you’ve still got questions, or this wasn’t clear enough. Good luck!

References
Codable Tutorial Even though it’s a year old, it is still perfectly relevant if you’re not familiar or comfortable with Codable.

Closures A great video reference on closures. This is the basic concept I used to handle the callbacks when reading the tags.

Thank you! I think this will be enough for me to figure these issues out.

Just one more question. In this tutorial you set up a button to summon the modal that prompts the user to scan a tag. Is it true that this is only necessary with iphones 7 & 8 and that generations after 8 can read tags without the prompt? and if so, can I conditionally set the prompt based on which device the user has?

The tutorial has the following to check whether your device can scan:
guard NFCNDEFReaderSession.readingAvailable else {

NFCNDEFReaderSession.readingAvailable is your way to wrap device supported code.

As far as not prompting the user, you can do background reading with the XS and beyond (per Apple’s documentation)

You can learn about Universal Links (for background reading) here

1 Like

I have compiled your code but I get this error I am using xcode 12 with iOS 14

[CoreNFC] 00000002 83fe0180 -[NFCNDEFReaderSession _callbackDidBecomeActive]:218 Delegate does not implement -readerSessionDidBecomeActive: method

This means your code does not have this protocol method implemented. Your can simply add it where the error is saying, or of it is ambiguous, where the other NFC reader session protocol methods are implemented.

Im coming across an issue when I try to write a tag. the tag is detected but I get this alert message: “NDEF tag does not contain any NDEF message”

I’ve tried using the final version of your code for the NeatoCache app and I get the same thing. I’ve tried the same thing a couple months ago with this tutorial while using xcode 11 and it worked. Now that I’m using xcode 12, it doesn’t. Any thoughts?

Did you end up resolving this issue? I’m getting a similar one.

In Admin tab Save location is not working…I am not getting popup Successfully setup location.