Guys, I need your help, please.
What bởi I need: đồ sộ get the tag's UID (i.e Serial number) using iOS app
Bạn đang xem: iphone 1k
What bởi I have: I have an tiện ích Flutter, which works fine on Android (I can read the tag's UID) and bởi 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 đồ sộ get from the app)
Xem thêm: chất nào sau đây là chất tinh khiết
Saved message on the tag: "My text with spaces"
On iOS side I created two implementations: one for NFCTagReaderSession, another one for NFCNDEFReaderSession (I bởi not use it at the same time, only separated)
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 điện thoại tư vấn.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 tiện ích, i can see the NFC reading window on my phone
But, when i put the thẻ up đồ sộ the phone, there is noting happened
Xem thêm: iphone 13 pro max mấy sim
// 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 điện thoại tư vấn.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 tiện ích, NFC reading window also shows up. And with the same thẻ 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 lượt thích there is no thẻ. 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 đồ sộ get the UID of the card?
Bình luận