问题描述
我正在尝试此代码,我想要的URL是www.tapaway.com.au,但是我得到的是https://%02tapaway.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”。URL为“ https://www.tapaway.com.au”。
因此,您需要正确解码有效负载,然后才能将其用作URL。
第一个字节是URI标识符。
其他所有字节都是缩短的URL
有关https://github.com/haldean/ndef/blob/master/docs/NFCForum-TS-RTD_URI_1.0.pdf的此类NDEF记录,请参阅规范文档。
似乎有一个方便的功能
https://developer.apple.com/documentation/corenfc/nfcndefpayload/3153117-wellknowntypeuripayload
大概(我不是一个敏捷的程序员)
var url = payload.wellKnownTypeURIPayload()