My code is
let idx = (self.tappedIndexPath?.row)!
let pcs = self.sortedArray[idx]
let urlW = URL(string: ("https://en.wikipedia.org/w/index.php?search=\(String(describing: pcs.name))".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed))!)
let svm = SFSafariViewController.init(url: urlW!)
self.navigationController?.pushViewController(svm, animated: true)
is crashing for this error:
"The specified URL has an unsupported scheme. Only HTTP and HTTPS URLs are supported."
I do not understand the problem, my url is in https so why it is crashing?
The error message is misleading. Too many percent escaped characters confuse the URL(string
initializer.
The solution is to percent escape only the search query using the proper character set ( urlHostAllowed
is only for the host, the part between ://
and the following slash) and for safety add optional bindings:
let pcs = self.sortedArray[idx]
if let query = pcs.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let urlW = URL(string: "https://en.wikipedia.org/w/index.php?search=" + query) {
let svm = SFSafariViewController(url: urlW)
self.navigationController?.pushViewController(svm, animated: true)
} else {
print("Bad URL")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.