iOS NFC read Mifare 1k classic tag's uid

Guys, I need your help, please.

What do I need: to get the tag's UID (i.e Serial number) using iOS app

What do I have: I have an app Flutter, which works fine on Android (I can read the tag's UID) and do nothing on iOS

The card's details (plastic card):

Tag type: ISO 14443-3A

Technologies available: NfcA, MifareClassic, Ndef

Serial number: AB:BF:88:AE (this is i really need to get from the app)

Saved message on the tag: "My text with spaces"

On iOS side I created two implementations: one for NFCTagReaderSession, another one for NFCNDEFReaderSession (I do not use it at the same time, only separated)

NFCTagReaderSession

import UIKit
import Flutter
import CoreNFC

@available(iOS 13.0, *)
var session_tag: NFCTagReaderSession?
@available(iOS 13.0, *)
var flutterResult: FlutterResult!

@available(iOS 13.0, *)
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, NFCTagReaderSessionDelegate {
    
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        
        let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
        let nfcChannel = FlutterMethodChannel(name: "samples.flutter.dev/nfc", binaryMessenger: controller.binaryMessenger)
        nfcChannel.setMethodCallHandler({
            [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
            // Note: this method is invoked on the UI thread.
            guard call.method == "getNFCTag" else {
                result(FlutterMethodNotImplemented)
                return
            }
            if NFCTagReaderSession.readingAvailable {
                print("we are ready for reading");
                self?.startReadingNFC(result: result)
            } else {
                print("sorry byt not working");
            }
        })
        
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    private func startReadingNFC(result: @escaping FlutterResult) {
        flutterResult = result
        session_tag = NFCTagReaderSession(pollingOption: .iso14443, delegate: self, queue: DispatchQueue.main)
        session_tag?.alertMessage = "Hold ur phone!"
        session_tag?.begin()
    }
    
    func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {
        print("Tag session active")
    }
    
    func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
        print("Did detect tag reader session")
        print(tags.count)
        if case let NFCTag.miFare(tag) = tags.first! {
            session.connect(to: tags.first!) { (error: Error?) in
                print(tag)
            }
        }
    }
    
    func tagReaderSession(_ session: NFCTagReaderSession, didInvalidateWithError error: Error) {
        print("Tag Error: \(error.localizedDescription)")
        
    }
}

When i'm pressing the button "read tags" in my app, i can see the NFC reading window on my phone

But, when i put the card up to the phone, there is noting happened

NFCNDEFReaderSession

// the same part
@available(iOS 13.0, *)
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, NFCNDEFReaderSessionDelegate {

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {

    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    let nfcChannel = FlutterMethodChannel(name: "samples.flutter.dev/nfc", binaryMessenger: controller.binaryMessenger)
    nfcChannel.setMethodCallHandler({
        [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
      // Note: this method is invoked on the UI thread.
      guard call.method == "getNFCTag" else {
        result(FlutterMethodNotImplemented)
        return
      }
      if NFCNDEFReaderSession.readingAvailable {
          print("we are ready for reading");
          self?.startReadingNFC(result: result)
      } else {
          print("sorry byt not working");
      }
    })

    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
     func startReadingNFC(result: @escaping FlutterResult) {
        print("start reading session")
        flutterResult = result
        session_tag = NFCNDEFReaderSession(delegate: self, queue: DispatchQueue.main, invalidateAfterFirstRead: false)
        session_tag?.alertMessage = NSLocalizedString("Hold it!", comment: "my comment")
        session_tag?.begin()
    }

    func tagReaderSessionDidBecomeActive(_ session: NFCNDEFReaderSession) {
        print("Tag session active")
    }

    func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
        print(messages)
      for message in messages {
        for record in message.records {
            print("next message")
            print(record)
        }
      }
    }

    func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
        print("Tag Error: \(error.localizedDescription)")
    }

    func tagReaderSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
        print("Tag Error: \(error.localizedDescription)")
    }
}

After pressing the button in the app, NFC reading window also shows up. And with the same card i can get the payload message in a console

Both approaches start fine and open the iOS's window with "read a tag", but with NFCTagReaderSession nothing is happening. It seems like there is no card. With NFCNDEFReaderSession implementation, I can read the message "My text with spaces" but, of course, I can't read the tag's UID

Am I doing something wrong to get the UID of the card?

Post not yet marked as solved Up vote post of Sergei_k Down vote post of Sergei_k
2.9k views

Replies

I have the same problem.

Hi, has anyone found a solution?

  • Well, basically, I wrote to Apple's team and got the answer now this is a technical limitation and there is no way to get that kind of information from tags

Add a Comment