Get Started with Reliable Robust Libraries
NFC apps on iOS & Android using Xamarin Forms or Native
Many of you must have heard/read about NFC, and possibly used it to pay using Google Wallet or Apple pay. As developers, we are gifted with the knowledge and ability to actually interact and create products that use such technologies. It may seem intimidating since you may not be familiar with electronics at first, but the good thing is that these cost about a dollar a piece, so it’s alright even if you blow one up. You can follow this article with no programming experience, as it breaks down how you can be an innovator who empowers your company to use NFC in their products & services.
What are these and how can I use them?
You can think of NFC tags as minuscule flash drives that you can use to wirelessly store and retrieve tiny bits of information from, that can also be made “read-only”. Hence, you can use it for various purposes- uniquely identifying people or shipments, unlocking special features in games when users buy memorabilia, tracking cash credits at festivals so you don’t have to worry about carrying and losing cash/cards. You can ensure security in various ways, and using mobile apps makes that much easier.
What are all the limitations?
There are many different types of RFID tags, so the iOS & Android support for them also varies. It’s important to understand and only purchase the NFC tags that are supported by both platforms, which until October 2019 was just — NDEF, MIFARE, ISO/IEC 14443, ISO/IEC 15693, ISO/IEC 7816 & FeliCa.
For iOS, iPhones 7 and later allow reading and writing to NFC tags, and only iPhones XS and later offer background tag reading. For Android, many new phones still don’t ship with NFC capabilities to maintain a low price, so you have to specifically look for it while purchasing.
Who will take my Loonies for NFC Tags?
To find the best tags to purchase, I searched “NFC Smart Tags” on the Chinese electronics site popular with tinkerers globally. I ended up purchasing these 3 different variations of the same thing:
Is programming these tags really that easy?
After I received my tags, I quickly searched for an NFC library/nuget package for Xamarin Forms and found this to be the most recently updated one. I ventured for a sample to it’s GitHub, cloned the repo to my Mac, opened the “Plugin.NFC.Full.sln” file in Visual Studio for Mac, changed the build config to “Android|Debug|Pixel”, and built the sample on my Android device. Once built I was able to read and write smoothly with lots of excitement and joy, after just a few changes to the short & easy to understand sample.
Why does Apple have to go and make things so complicated?
Next, I switched the config to iPhoneSimulator|Debug|iPhone 8 and tried to build it on the iPhone simulator, which worked, but obviously it notified me that I didn’t have NFC on the simulator. So, I tried to build the app on my iPhone, but it would not build because of the following error:
Your code signing/provisioning profiles are not correctly configured. Probably you have an entitlement not supported by your current provisioning profile, or your device is not part of the current provisioning profile
Luckily I was prepared, and in the Apple Developer portal, I already activated NFC as an entitlement for my app and downloaded and installed those certificates, so I just needed to change the sample bundle ID to my existing app. Alternatively, you can use free provisioning. And, then it worked perfectly, with just a few differences from Android, which will be explained in the next section.
How does the app interact with the NFC Tag?
What you see in the screenshots of the sample is the Main page of the app, followed by what happens when you tap on one of the 5 buttons. There are 3 types of operations you can perform on the tag using the library — write, read and clear. There are three things you can write using the app — text, url , custom data. If you want to make any changes to the data that is written, you just have to do that in the
Differences between iOS & Android
There are a few key differences in the functionality, that are of interest to cross-platform developers and consultants:
The “Ready to Scan” dialog
As you can see from the screenshots, reading the RFID tags requires telling the app to Read the Tag, which start listening for NFC tags. Listening for NFC tags, requires the iOS app to launching the standard “Ready to Scan” dialog. iOS doesn’t give permissions to change much inside it, and it times out if it doesn’t come near a tag before cancelling. The Android app keeps “listening” for the tag without any indication, unless you manually add one (like this), and has a much longer timeout.
Background Tag Reading
In iOS devices that support background reading, you do not need to unlock, or open the app to read tags that have URL’s, and it will just use the Safari browser to open the URL’s. To make the URL open your app, you can use universal links or contextual deep links. According to Apple:
Background tag reading doesn’t support custom URL schemes. Use universal links instead.
In Android devices, you cannot read tags while your phone is locked, but you do not need to open the app to read any of the tags. You can force your app to handle/read the tag first though.
Do I need to enable NFC?
iOS has NFC enabled by default. To enable it on an Android device that has NFC, you need to go to your Settings->Connection Preferences -> Allow NFC Data Exchange
- Sean Sparksman did a deep dive on NFC’s and Xamarin at the Xamarin Developer Summit in 2019, that also made it to Channel 9. There are a few changes to the technology since then, especially being able to write using iOS devices.
- Huge should out to Frank Bour, without whose library I would still be figuring this out.
- I created a short video that explains this all with much more illustrations, and a demo: