Chao Chao - 3 months ago 18
Swift Question

WKWebView don't show the website in swift with iOS 9.0

i try to use WKWebView, but it don' work.

here is my code

import UIKit
import WebKit

class WebviewVC: UIViewController, WKNavigationDelegate {

var webview: WKWebView?

// MARK: - ********** Lifecycle **********

override func viewDidLoad() {
super.viewDidLoad()

webview = WKWebView(frame: view.bounds)
view.addSubview(webview!)
webview!.navigationDelegate = self
loadUrl("http://www.google.com")
webview!.allowsBackForwardNavigationGestures = true
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

//MARK: - ********** load url ***********

func loadUrl(urlStr: String) {
let url = NSURL(fileURLWithPath: urlStr)
let requestObj = NSURLRequest(URL: url)
webview!.loadRequest(requestObj)
}


}


i have seted the App Transport Security Settings -> Allow Arbitrary Loads to YES. but the webView also don't show the website.

pls help!

Lu_ Lu_
Answer

What is this:

let url = NSURL(fileURLWithPath: urlStr)

you should use:

let url = NSURL(string: urlStr)