url 在 iOS 中解码的问题
Issue with url Decode in iOS
我正在尝试解码具有另一个 url(单独编码)作为查询参数的 url:
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
然而,下面的代码似乎是"double decoding"两次编码url。
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({ [=13=].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
为什么会出现 "double decoding",我该如何解决?
尝试从下面的行中删除 removingPercentEncoding
let urlString = queryItems.filter({ [=10=].name == "url" }).first?.value?.removingPercentEncoding
我正在尝试解码具有另一个 url(单独编码)作为查询参数的 url:
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
然而,下面的代码似乎是"double decoding"两次编码url。
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({ [=13=].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
为什么会出现 "double decoding",我该如何解决?
尝试从下面的行中删除 removingPercentEncoding
let urlString = queryItems.filter({ [=10=].name == "url" }).first?.value?.removingPercentEncoding