我在Web服务器中有一个正在运行的应用程序,它重定向到安装在iOS 14设备上的应用程序。url的形式是
myapp://?p=someBase64EncodedString
并且该应用程序正在对该字符串进行解码。升级到iOS 14后,应用程序获得的url为
myapp://?p=someBase64EncodedString#
添加到字符串末尾的英镑符号在我的设备上无法解码。当使用ignoreUnknownCharacters
时,一切都很好,但这个#
是从哪里来的?
正如评论中提到的@jtbandes,#
是URL中的有效字符。
为了避免这种问题,解析URL最安全的方法是使用URLComponents,遵循以下答案。
这里有一个例子:
let urlString = "https://test.host.com:8080/Test/Path?test_parameter=test_value#test-fragment"
let urlComponents = URLComponents(string: urlString)
print("scheme: (urlComponents?.scheme ?? "nil")")
print("host: (urlComponents?.host ?? "nil")")
print("port: (urlComponents?.port ?? -1)")
print("path: (urlComponents?.path ?? "nil")")
print("query: (urlComponents?.query ?? "nil")")
print("queryItems: (urlComponents?.queryItems ?? [])")
print("fragment: (urlComponents?.fragment ?? "nil")")
这将作为输出:
scheme: https host: test.host.com port: 8080 path: /Test/Path query: test_parameter=test_value queryItems: [test_parameter=test_value] fragment: test-fragment
Swift 5和原始字符串会是罪魁祸首吗?我的猜测是,应用程序使用#作为自定义字符串分隔符,在解码时用#替换一些特殊字符。
它可能是您使用的api重定向中的一个api操作。如果是这样的话,它就超出了您的控制范围,也可能超出您的访问范围。