Read NFC Tag UID in appDelegate when scanning tags from background in iOS swift

Viewed 871

I want to write an iOS application that reads NFC Tags in the background. Based on the available documents I can scan NFC tags from the background and everything is fine. But my problem is that I want to get UID of tag in my app delegate as well. This is my method in the app delegate.

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {

NSLog(@"%@", [userActivity userInfo]);
NSLog(@"%@", [userActivity ndefMessagePayload]);
NSLog(@"%@", [[userActivity ndefMessagePayload] records]);
NSLog(@"%@", [[userActivity ndefMessagePayload] description]);
NSData *payload = [[[[userActivity ndefMessagePayload] records] firstObject] payload];

nfcPayload = [[NSString alloc] initWithData:payload encoding:NSASCIIStringEncoding] ; // this is my scaned payload


return true;
}                                                                    

And in this is my log for nDeMessagePayload:

"TNF=1, Payload Type={length = 1, bytes = 0x55}, Payload ID={length = 0, bytes = 0x}, Payload={length = 24, bytes = 0x0474656c656772616d322e666172616e656761722e636f6d}, ChunkSize=4294967295"

My payload ID is zero bytes and I cannot get UID of NFC Tag. I know how to read UID of the NFC tags from the session when reading not from the background but getting the UID from background scanning is my case. what's the problem?

1 Answers

I had the same problem and it looks like you only get access to the NDEF payload, without the UID when background scanning.

My solution was to open the app and trigger scanning again to get the UID.

Related