从NFC mifare标签iOS 13读取UID [英] Read UID from NFC mifare tag iOS 13

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

问题描述

我正在尝试读取mifare标签的UID.

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.

我还注意到xCode发出警告,该警告几乎匹配一个可选协议,该协议除了使用 NFCNDEFTag 而不是 NFCTag 之外,都是相同的..

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..

当我尝试更新时,该方法确实被调用.但是然后我在尝试将标签定义为该方法中的mifare时遇到了麻烦.我收到错误消息模式不能匹配'NFCNDEFTag'类型的值.

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'.

以下代码:

    @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?

                    }
            }
        }
    }

推荐答案

您初始化的会话应该是NFCTagReaderSession

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

您应该遵守NFCTagReaderSessionDelegate https://developer.apple. com/documentation/corenfc/nfctagreadersessiondelegate?language = objc

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆