How to Get the Tag Identifier in Core NFC

3.1k views Asked by At

How can I get the ID of an NFC tag (not the ID of the message payload) using Core NFC?

I am looking for something similiar to this function that exists in Android: https://developer.android.com/reference/android/nfc/Tag.html#getId()

2

There are 2 answers

1
GreuM On

Here is how you can achieve it. But keep in mind that it uses a private function, that can be removed / changed at any time by Apple, and can cause rejection from the AppStore.

Tested in my app, it works for now with iOs 11.1

The sources :

https://github.com/hansemannn/iOS11-NFC-Example/issues/16

https://github.com/chariotsolutions/phonegap-nfc/pull/287/files#diff-84fad93feff6a327c30a08cac8f546dfR171

        func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
            let uid : String = getTagIdFromSession(session: session)
            //Do what you want with the UID

        }


        func getTagIdFromSession(session : NFCNDEFReaderSession) -> String{
            var uid: String = ""
            if(session.value(forKey: "_foundTags") != nil) {
                let foundTags : NSArray = session.value(forKey: "_foundTags") as! NSArray
                if(foundTags.count > 0) {
                    let tag : NSObject = foundTags.firstObject  as! NSObject;
                    if(tag.value(forKey: "_tagID") != nil) {
                        var uuidPadded : Data = tag.value(forKey: "_tagID") as! Data
                        //We reverse the order
                        for (i,_) in uuidPadded.enumerated() {
                            uuidPadded.insert(uuidPadded.remove(at:i),at:0)
                        }
                        for (_, element) in uuidPadded.enumerated() {
                            let tag : String = String(element, radix:16)
                            //We add the missing 0 in case the number is < 10. It can be done with bitwise operations too.  
                            if(tag.length < 2) {
                                uid.append("0"+tag)
                            }
                            else {
                                uid.append(tag)
                            }


                        }
                    }
                }
            }
            return uid;
        }
0
parvus On

The use of _foundTags will cause rejection:

1.2 Binary Rejected

Guideline 2.5.1 - Performance - Software Requirements

Your app uses or references the following non-public APIs:

_foundTags

The use of non-public APIs is not permitted on the App Store because it can lead to a poor user experience should these APIs change.

Continuing to use or conceal non-public APIs in future submissions of this app may result in the termination of your Apple Developer account, as well as removal of all associated apps from the App Store.

Next Steps

If you are using third-party libraries, please update to the most recent version of those libraries. If you do not have access to the libraries' source, you may be able to search the compiled binary using the "strings" or "otool" command line tools. The "strings" tool can output a list of the methods that the library calls and "otool -ov" will output the Objective-C class structures and their defined methods. These tools can help you narrow down where the problematic code resides. You could also use the "nm" tool to verify if any third-party libraries are calling these APIs.

Resources

For information on the "nm" tool, please review the "nm tool" Xcode manual page.

If there are no alternatives for providing the functionality your app requires, you can file an enhancement request.