我尝试将另一个网址(单独编码)的网址解码为查询参数:
com.testScheme://openurl?url=https%3A%2F%2Fm%2Euber%2Ecom%2Ful%3Faction%3DsetPickup%26pickup%3Dmy%5Flocation%26dropoff%5Bformatted%5Faddress%5D%3D5394%2520General%2520Hood%2520Trl%2C%2520Nashville%2520TN
然而,下面的代码似乎是"双解码"两次编码的网址。
guard let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false),
let requestType = urlComponents.host else {
return false
}
if requestType.lowercased() == "openurl" {
guard let queryItems = urlComponents.queryItems,
let urlString = queryItems.filter({ $0.name == "url" }).first?.value?.removingPercentEncoding,
let url = URL(string: urlString) else {
//unable to get parameters
return false
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.milliseconds(25)) {
UIApplication.shared.openURL(url)
}
return true
}
urlString
的输出:
https://m.uber.com/ul?action=setPickup&pickup=my_location&dropoff[formatted_address]=5394 General Hood Trl, Nashville TN
我需要urlString
:
https://m.uber.com/ul?action=setPickup&pickup=my_location&dropoff[formatted_address]=5394%20General%20Hood%20Trl,%20Nashville%20TN
为什么"双解码"发生了,我该如何解决?
答案 0 :(得分:2)
尝试从以下行中删除removingPercentEncoding
let urlString = queryItems.filter({ $0.name == "url" }).first?.value?.removingPercentEncoding