本文介绍了在iOS 13中读取NFC卡的UID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索MiFare卡的UID.我正在使用iPhone X,Xcode 11和iOS13.

I would like to retrive the UID of MiFare cards. I'm using an iPhone X, Xcode 11 and iOS 13.

根据此网站,我知道直到iOS 13才有可能(特别是读取UID): https://gototags.com/blog/apple-expands-nfc-on-iphone-in-ios-13/和这个家伙: https://www.reddit.com/r/apple/comments/c0gzf0/clearing_up_misunderstandings_and/

I'm aware this wasn't possible (specifically reading the UID) until iOS 13 according to this website: https://gototags.com/blog/apple-expands-nfc-on-iphone-in-ios-13/ and this guy: https://www.reddit.com/r/apple/comments/c0gzf0/clearing_up_misunderstandings_and/

电话NFC读取器正在正确检测到卡,但是唯一标识符始终返回为空或nil.我可以读取有效载荷,但与iOS无关,但是我可以在Android中读取(确认卡没有问题或只是奇怪)

The phones NFC reader is correctly detecting the card however the unique identifier is always returned as empty or nil. I can read the payload however and irrelvant to iOS but I can do this in Android (confirms the card isn't faulty or just odd)

Apple示例项目: https://developer.apple.com/documentation/corenfc/building_an_nfc_tag-reader_app

Apple Sample Project: https://developer.apple.com/documentation/corenfc/building_an_nfc_tag-reader_app

    func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
            if case let NFCTag.miFare(tag) = tags.first! {
                session.connect(to: tags.first!) { (error: Error?) in
                    let apdu = NFCISO7816APDU(instructionClass: 0, instructionCode: 0xB0, p1Parameter: 0, p2Parameter: 0, data: Data(), expectedResponseLength: 16)
                    tag.queryNDEFStatus(completionHandler: {(status: NFCNDEFStatus, e: Int, error: Error?) in
                        debugPrint("\(status) \(e) \(error)")
                    })
                    tag.sendMiFareISO7816Command(apdu) { (data, sw1, sw2, error) in
                        debugPrint(data)
                        debugPrint(error)
                        debugPrint(tag.identifier)
                        debugPrint(String(data: tag.identifier, encoding: .utf8))
                    }
                }
            }
        }

我知道这类黑客: CoreNFC无法在iOS中读取UID

但是它们已关闭,并且仅在过去很短的时间内适用于iOS 11.

But they are closed and only apply to iOS 11 for a short time in the past.

推荐答案

好的,我有一个答案.

Ok I have an answer.

tag.identifier不为空-本身-如果您从Xcodes调试器检查,则它为空(值为0x00!).它的类型是Data,打印它会显示Data的长度,而不是它的编码方式.在这种情况下,它是一个[UInt8],但存储为一袋零碎的东西,我不明白为什么Apple这样做-笨重的-我相信他们有充分的理由.我将其存储为String类型-毕竟,像Swift这样的高级语言的全部要点是使我们摆脱了此类hadware实现细节.

tag.identifier isn't empty -- per se -- if you examine from Xcodes debugger it appears empty (0x00 is the value!). It's type is Data and printing it will reveal the length of the Data but not how it's encoded. In this case it's a [UInt8] but stored as a bag of bits, I don't understand why Apple have done it this way -- it's clunky -- I'm sure they have good reasons. I would have stored it as a type String -- after all the whole point of a high level language like Swift is to abstract us away from such hadware implementation details.

以下代码将从MiFare卡中检索UID:

The following code will retrive the UID from a MiFare card:

            if case let NFCTag.miFare(tag) = tags.first! {
                session.connect(to: tags.first!) { (error: Error?) in
                    let apdu = NFCISO7816APDU(instructionClass: 0, instructionCode: 0xB0, p1Parameter: 0, p2Parameter: 0, data: Data(), expectedResponseLength: 16)
                    tag.sendMiFareISO7816Command(apdu) { (apduData, sw1, sw2, error) in
                        let tagUIDData = tag.identifier
                        var byteData: [UInt8] = []
                        tagUIDData.withUnsafeBytes { byteData.append(contentsOf: $0) }
                        var uidString = ""
                        for byte in byteData {
                            let decimalNumber = String(byte, radix: 16)
                            if (Int(decimalNumber) ?? 0) < 10 { // add leading zero
                                uidString.append("0\(decimalNumber)")
                            } else {
                                uidString.append(decimalNumber)
                            }
                        }
                        debugPrint("\(byteData) converted to Tag UID: \(uidString)")
                    }
                }
            }

这篇关于在iOS 13中读取NFC卡的UID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!