ios - 如何解析Type U的NFC数据

标签 ios swift parsing nfc

我正在尝试 parse来自格式化 NFC chip 的有效负载但我遇到了一个问题 record.type == U

我正在使用带有应用程序的 Android 手机 NXP writer , 写我的 NFC 芯片。 我得到了 record.type == T 的一切,但是当我得到 record.type == U ,我遇到了一些困难。

我在网上找到的想法是,转换 data to hexStringchecking the first 2 numbers , 那么您可以对 URI 可以具有的以下类别进行分类。

iOS 11 - How does one read/parse the NDEF Message from CoreNFC?

例子:

  1. 0x00 不适用。没有预先准备好
  2. 0x01 http://www .
  3. 0x02 https://www .
  4. 0x03 http://
  5. 0x04 https://
  6. 0x05 电话:
  7. 0x06 邮寄地址:
  8. 0x07 ftp://anonymous:anonymous@
  9. 0x08 ftp://ftp .

等等

hexString = "02676f6f676c652e636f6d"

子串 = "02"

然后我们有 https://www然后我们从有效负载中添加字符串,然后打开 safari(或者我们想做的任何事情)。

The problem comes when I want to read a NFC Tag/chip that contains Tel, mailto, geo, sms so far.

The code is 3 digits 006 而不是我在网上阅读的 2 位数字。不仅如此,如果我决定进行地理定位,I get 006 which is the same with mailto code .当然我可以用更多的代码来解决这个问题,但是由于有些解决方案只检查那些代码,我能做些什么吗,还是我做错了什么?

问题:

  1. NXP writer 有问题吗?我正面临这个问题?
  2. NFC 编码是否已更改?
  3. 我的逻辑正确吗?

这是我的代码:

    func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
    session.invalidate()


    for message in messages{

        for record in message.records{

            guard record.payload.count > 0 else {
                print("Record payload doesn't contain any data in the memory")
                session.invalidate()
                return
            }

            guard let NFCTypeFromTag = String(data: record.type, encoding: .utf8) else{
                print("Unable to detect the Type")
                session.invalidate()
                return
            }

            guard let nfcTypeInput = NFCType(rawValue: NFCTypeFromTag) else {
                print("nfc enum problem")
                session.invalidate()
                return
            }

            switch nfcTypeInput{
            case .T:
                guard let NFCStringFromPayload = String(data: record.payload.advanced(by: 3), encoding: .utf8) else {
                    return
                }
                NFCString = NFCStringFromPayload
            case .U:
                guard let NFCStringFromPayload = String(data: record.payload, encoding: .utf8) else{
                    print("Error: Unable to convert record.payload to String")
                    return
                }

                let hexString = record.payload.hexEncodedString()
                let indexEndFromHexString = hexString.index(hexString.startIndex, offsetBy: 2)

                let substring = hexString[hexString.startIndex ..< indexEndFromHexString]

                print("NFCString : \(NFCStringFromPayload)")
                print("HexString : \(hexString)")
                print("Substring : \(substring)")

                if substring == "00"{
                    let indexEndOfHexStringCase00 = hexString.index(hexString.startIndex, offsetBy: 3)
                    let substringCase00 = hexString[hexString.startIndex..<indexEndOfHexStringCase00]
                    if substringCase00 == "005" {
                        print("Tel:")
                    }else if substringCase00 == "006"{
                        print("mailto:")
                    }else if substringCase00 == "007" {
                        print("sms")
                    }
                }else if substring == "01"{
                    print("http://www")
                }else if substring == "02"{
                    print("https://www")
                }

                NFCString = NFCStringFromPayload

           case .Sp:
                break
            }
            NFCArray.append(NFCString!)
            tableView.reloadData()
            print("NFC String : \(NFCString)")

        }
    }

Apple 在 CoreNFC 上有糟糕的文档。

是否有人实现过类似的解决方案。

提前谢谢你。

最佳答案

可以直接使用Data,不需要使用String十六进制转换。

func parseURINFC(_ data: Data) -> String? {
    let prefix = data.prefix(1)
    let rest = data.dropFirst(1)

    switch prefix {
    case Data(bytes: [0x00]):
        return nil
    case Data(bytes: [0x01]):
        guard let restString = String(data: rest, encoding: .utf8) else { return nil }
        return "http://www." + restString
    case Data(bytes: [0x02]):
        guard let restString = String(data: rest, encoding: .utf8) else { return nil }
        return "https://www." + restString
    case Data(bytes: [0x03]):
        guard let restString = String(data: rest, encoding: .utf8) else { return nil }
        return "http://" + restString
    case Data(bytes: [0x04]):
        guard let restString = String(data: rest, encoding: .utf8) else { return nil }
        return "https://" + restString
    case Data(bytes: [0x05]):
        guard let restString = String(data: rest, encoding: .utf8) else { return nil }
        return "tel://" + restString
    case Data(bytes: [0x06]):
        guard let restString = String(data: rest, encoding: .utf8) else { return nil }
        return "mailto://" + restString
    case Data(bytes: [0x07]):
        guard let restString = String(data: rest, encoding: .utf8) else { return nil }
        return "ftp://anonymous:anonymous@" + restString
    case Data(bytes: [0x08]):
        guard let restString = String(data: rest, encoding: .utf8) else { return nil }
        return "ftp://ftp." + restString
    default:
        return nil
    }
}

关于 006-007- 前缀,根据您提供的链接 ( http://austinblackstoneengineering.com/nfc-p2p-basics/ ),它是无效的。尝试使用 UTF8 转换其余部分后的事件(因为它是一个 URI,它应该如此)它返回 nil。

关于ios - 如何解析Type U的NFC数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54238027/

相关文章:

ios - 当前点的图像大于其余点的自定义页面控件不起作用

swift - 分页垂​​直滚动 WatchKit

swift - 不为 NSMenu 调用 validateMenuItem 或 menuWillOpen

algorithm - 解析具有未定义数量的参数的表达式

java - 无法抓取 HTML 网站?

ios - UIButton 的系统镜像出现在 iOS14 中,但不在 iOS13 中

ios - 是否可以在没有 IOS SDK 的情况下安装 xcode macOS sdk?

ios - 升级到 Xcode 7 会在 CoreData 模型上创建 "Missing current version declaration"警告

用于登录 Twitter 的 iOS 库

php - 如何通过输出数组的每一行的特定一个属性来运行 htmlentities()