将NFC标签的有效载荷转换为URL会给URL添加额外的字符,并使其成为无效地址



我正在尝试这个代码,我想要的URL是www.tapaway.com.au,但我得到的是https://%02apaway.com.au。我在NFC标签上写了第一个URL www.tapaway.com.au,所以每当有人扫描标签时,它都会打开我的网页。我的代码是

func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
var result = ""
for payload in messages[0].records{
print("-> (payload)") //Prints the Entire NFC Payload on the Tag.
result += String.init(data: payload.payload, encoding: .utf8) ?? ""
}
DispatchQueue.main.async {
var components = URLComponents()
components.scheme = "https"
components.host = "(result)"
components.path = "/"
let url = components.url
print(url)
UIApplication.shared.open(url!)
}
}

因此,URL以编码形式存储在NDEF记录中以节省空间。

%02是用于URI标识符的代码;https://www."在这种情况下
tapaway.com.au是URI有效载荷

因此,当正确地解码URL时;https://www.tapaway.com.au"您将添加";https";再次站在前面。

因此,在将有效负载用作URL之前,您需要正确地对其进行解码
第一个字节是URI标识符
所有其他字节都是缩短的URL

有关此类NDEF记录,请参阅规范文件https://github.com/haldean/ndef/blob/master/docs/NFCForum-TS-RTD_URI_1.0.pdf

这个似乎有一个方便功能

https://developer.apple.com/documentation/corenfc/nfcndefpayload/3153117-wellknowntypeuripayload

所以很可能(我不是一个敏捷的程序员(

var url = payload.wellKnownTypeURIPayload()

最新更新