Read UID from NFC mifare tag iOS 13

蹲街弑〆低调 提交于 2020-01-25 08:14:39

问题


I'm trying to read the UID for a mifare tag.

Looking at examples, I see the following method a lot:

func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {      
  if case let NFCTag.miFare(tag) = tags.first! {
    print(tag.identifier as NSData)
  }
}

However when putting that into my project, the method is not called.

I also noticed xCode giving a warning that this nearly matches an optional protocol which is the same except it uses NFCNDEFTag instead of NFCTag..

When I try update to that,that method does get called. But then I'm having trouble trying to define the tag as mifare inside that method. I get the error 'Pattern cannot match values of type 'NFCNDEFTag'.

Code below:

    @available(iOS 13.0, *)
    func readerSession(_ session: NFCNDEFReaderSession, didDetect tags: [NFCNDEFTag]) {
        print("in did detect tags")

        let tag = tags.first!

        session.connect(to: tag) { (error: Error?) in


            session.connect(to: tag) { (error: Error?) in
                   if case let .mifare(mifareTag) = tag {
                       // can access tag identifier here?

                    }
            }
        }
    }

回答1:


Your session that you initialise should be a NFCTagReaderSession https://developer.apple.com/documentation/corenfc/nfctagreadersession

And you should be conforming to NFCTagReaderSessionDelegate https://developer.apple.com/documentation/corenfc/nfctagreadersessiondelegate?language=objc



来源:https://stackoverflow.com/questions/58285425/read-uid-from-nfc-mifare-tag-ios-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!