Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: How get tag uid? #55

Open
cristiandaulisio opened this issue Apr 29, 2020 · 18 comments
Open

iOS: How get tag uid? #55

cristiandaulisio opened this issue Apr 29, 2020 · 18 comments

Comments

@cristiandaulisio
Copy link

NfcNdefData should contain the id. In android it work but in iOS it is empty.

Is it possible get the tag uid?

@Puschor
Copy link

Puschor commented May 17, 2020

I have the same problem. Is there any workaround available? This is what I found so far regarding this issue:

https://stackoverflow.com/questions/57978507/reading-uids-of-nfc-cards-in-ios-13
https://stackoverflow.com/questions/51822226/corenfc-not-reading-uid-in-ios

Is this maybe related to iOS 13?

I tried to read the tag with a different iOS app and it properly shows the uid.

@demetrius-tech
Copy link

I started to work on this inspired by the phonegap-nfc plugin: https://github.com/chariotsolutions/phonegap-nfc/blob/master/src/ios/NfcPlugin.m

But, being a newbie in NativeScript, I have some issues and I need some help, please.
I cannot access some properties from the NFCTag objects.

The code can be found here and I have commented what issues I have:
https://github.com/demetrius-tech/nativescript-nfc/tree/dev

I am using demo-ng app.

Plugin iOS file is here: https://github.com/demetrius-tech/nativescript-nfc/blob/dev/src/nfc.ios.ts

Check tagReaderSessionDidDetectTags() and getTagUID() methods.

@demetrius-tech
Copy link

Does anyone know how to translate this Objective C code to TypeScript?

id tag = [tags firstObject];
NSMutableDictionary *tagMetaData = [self getTagInfo:tag];

Simply doing: const tag = tags[0]; does not return anything in tag.type

Here is what i've done so far: https://github.com/demetrius-tech/nativescript-nfc/blob/dev-uid/src/nfc.ios.ts (tagReaderSessionDidDetectTags() method)

@EddyVerbruggen
Copy link
Owner

EddyVerbruggen commented Jun 23, 2020

@demetrius-tech have you tried this? const tag = tags.firstObject

@demetrius-tech
Copy link

demetrius-tech commented Jun 23, 2020

@EddyVerbruggen: firstObject only works if I do like this: const tag = NSArray.arrayWithArray(tags).firstObject; otherwise it is not available.

Even so, if I do console.log(tag.type) it says "undefined".

console.log(tag) outputs "<NFCMiFareTag: 0x282d7e9a0>"

Probably more processing is needed on the tags[0] to extract the "type" and after that the "identifier" on the MiFare type.

@demetrius-tech
Copy link

Found a way to get the type, I will just compare this:

if (NFCTag.prototype.asNFCMiFareTag.apply(tag) === tag) {
console.log("THIS IS A MiFare tag");
tag.type = NFCTagType.MiFare;
let mifareTag:NFCMiFareTag = NFCTag.prototype.asNFCMiFareTag.apply(tag);
uid = NSData.alloc().initWithData(mifareTag.identifier);
} else if (NFCTag.prototype.asNFCISO15693Tag.apply(tag) === tag) {
console.log("ISO15693Tag");
} else if (NFCTag.prototype.asNFCISO7816Tag.apply(tag) === tag) {
console.log("NFCISO7816Tag");
} else if (NFCTag.prototype.asNFCFeliCaTag.apply(tag) === tag) {
console.log("FeliCaTag")
} else {
console.log("Unknown Tag");
}

Next step is to extract the identifier.

@demetrius-tech
Copy link

@demetrius-tech
Copy link

I've managed to get the tag UID by adding native code ObjC library.

You can check here: https://github.com/demetrius-tech/nativescript-nfc/tree/dev-ios-write-uid/src

Also, you can find there the code for writing tags for iOS.

If interested in PR, please let me know.

@cristiandaulisio
Copy link
Author

Hello demetrius, so you succesfull? can you explain how you did and where i retreive the tag id?

I've managed to get the tag UID by adding native code ObjC library.

You can check here: https://github.com/demetrius-tech/nativescript-nfc/tree/dev-ios-write-uid/src

Also, you can find there the code for writing tags for iOS.

If interested in PR, please let me know.

@demetrius-tech
Copy link

@Atomico001: you can find the ObjC code here: https://github.com/demetrius-tech/nativescript-nfc/tree/dev-ios-write-uid/src/platforms/ios/xcode/NfcNativeHelper

Created the libNfcNativeHelper.a library and added it to the src/platforms/ios (together with a .d.ts spec) : https://github.com/demetrius-tech/nativescript-nfc/tree/dev-ios-write-uid/src/platforms/ios

@cristiandaulisio
Copy link
Author

@Atomico001: you can find the ObjC code here: https://github.com/demetrius-tech/nativescript-nfc/tree/dev-ios-write-uid/src/platforms/ios/xcode/NfcNativeHelper

Created the libNfcNativeHelper.a library and added it to the src/platforms/ios (together with a .d.ts spec) : https://github.com/demetrius-tech/nativescript-nfc/tree/dev-ios-write-uid/src/platforms/ios

Good job demetrius, so i need to add manually in my platform folder? Any chance you can write a little tutorial to explain which step we need to do? Time to edit your readme :)

@demetrius-tech
Copy link

Thanks @Atomico001! I still hope to find a way to avoid using native code.

You will find the generic tutorial on how to add native code to NS plug-in here: https://docs.google.com/document/d/1_PDEkC3EtZCsXhthGCmGwtCVVxOwdAepcJaUaA0Uk6k/edit?usp=sharing

Please comment if something is not clear or any issues.

@cristiandaulisio
Copy link
Author

cristiandaulisio commented Jul 14, 2020

Thanks @demetrius-tech unlucky when i try to install your fork i receive this error when try to start nfc reader:

00000002 816faa00 -[NFCHardwareManager queueReaderSession:sessionConfig:completionHandler:]:98  error=Error Domain=NFCError Code=202 "Session invalidated unexpectedly" UserInfo={NSLocalizedDescription=Session invalidated unexpectedly}

00000002 838f6300 -[NFCTagReaderSession beginSessionWithConfig:]:324  error:Error Domain=NFCError Code=202 "Session invalidated unexpectedly" UserInfo={NSLocalizedDescription=Session invalidated unexpectedly}, errorCode: 0xca

@cristiandaulisio
Copy link
Author

any news?

@cristiandaulisio
Copy link
Author

I realy need this, someone can help me?

@demetrius-tech
Copy link

@Atomico001: I would like to help you but I can't find time. As I mentioned before I created a library in ObjectiveC and imported it to the plug-in. But I am not sure it is the best way, so I am looking for a better way to do this, may be a POD or something. I will try to allocate some time to this soon, will let you know.

@cristiandaulisio
Copy link
Author

@demetrius-tech nice to see you again. If you can do a working simple demo with your fork i can investigate it and try myself. Unlucky i receive many error when i tried to import your fork.

Thanks very much!

@PaulHieltjesCortegs
Copy link

@Atomico001: you can find the ObjC code here: https://github.com/demetrius-tech/nativescript-nfc/tree/dev-ios-write-uid/src/platforms/ios/xcode/NfcNativeHelper

Created the libNfcNativeHelper.a library and added it to the src/platforms/ios (together with a .d.ts spec) : https://github.com/demetrius-tech/nativescript-nfc/tree/dev-ios-write-uid/src/platforms/ios

So I tried the same thing as you and was also stuck on the issue that identifier is always null. Then I read this and thanks to your code got it working. Thank you very much!!!
Your typescript code did generate runtime errors on my device though so I ended up using my own code but with your iOS native code package.
Again, can't thank you enough for sharing your code here :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants