我传入一个字符串变量来打开webview中的url,当我根据字符串创建url时,它不会解包。我不确定我做错了什么。这是代码:
class WebViewController: UIViewController, WKUIDelegate {
var urlString:String?
var vehicle:Vehicle?
var theUrlString = "http://www.ksl.com/auto/search/" //This variable is set in the prepareForSegue in a previous view controller. It is set correctly
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
if let urlStri = urlString {
print("url is " + urlStri)
theUrlString = urlStri
}else {
}
}
override func viewDidLoad() {
super.viewDidLoad()
print("theUrlString is " + theUrlString) // this correctly prints: theUrlString is http://www.ksl.com/auto/search/index?keyword=&make%5B%5D=Chevrolet&model%5B%5D=Silverado 1500&yearFrom=2006&yearTo=2008&mileageFrom=&mileageTo=&priceFrom=&priceTo=&zip=&miles=25&newUsed%5B%5D=All&sellerType%5B%5D=&postedTime%5B%5D=&titleType%5B%5D=&body%5B%5D=&transmission%5B%5D=&cylinders%5B%5D=&liters%5B%5D=&fuel%5B%5D=&drive%5B%5D=&numberDoors%5B%5D=&exteriorCondition%5B%5D=&interiorCondition%5B%5D=&cx_navSource=hp_search
if let url = URL(string: theUrlString){
let myRequest = URLRequest(url: url) //In debugging, it never makes it inside the if statement here
webView.load(myRequest)
}
}
答案 0 :(得分:1)
您的DELETE h
-------^
FROM main_history h JOIN
main_iteminstance i
ON h.instance_id = i.id
WHERE platform_type_id = 'vudu';
编码不正确。因此,当您使用theUrlString
时,它将返回URL(string:)
(表示传入的URL字符串格式错误)。
我建议使用nil
来创建您的网址。
类似的东西:
URLComponents
var urlComponents = URLComponents(string: "http://www.ksl.com/auto/search/index")
var arguments: [String: String] = [
"keyword": "",
"make": "Chevrolet",
"model": "Silverado 1500"
]
var queryItems = [URLQueryItem]()
for (key, value) in arguments {
queryItems.append(URLQueryItem(name: key, value: value))
}
urlComponents?.queryItems = queryItems
if let url = urlComponents?.url {
print(url) // http://www.ksl.com/auto/search/index?keyword=&model=Silverado%201500&make=Chevrolet
}
API参考:https://developer.apple.com/reference/foundation/urlcomponents