Reputation: 27497
I have a controller that has an instance of WKWebView. However, I want it to not load any requests to Yahoo. It used to work with UIWebView (commented out below). I can't figure out how to do it using WKWebView.
class WebBrowserViewController: UIViewController {
let webView = WKWebView()
override func loadView() {
super.loadView()
self.view = self.webView
}
override func viewDidLoad() {
webView.navigationDelegate = self
let url = NSURL(string: "https://www.google.com")!
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
}
}
extension WebBrowserViewController: WKNavigationDelegate {
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
// this prints the right thing, but it doesn't allow me to stop the request
print(webView.URL?.absoluteString)
}
}
// WHAT I USED TO HAVE:
//extension WebBrowserViewController: UIWebViewDelegate {
// func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//
// if visitingYahoo(request.URL?.absoluteString) {
// return false
// } else {
// return true
// }
// }
//}
Upvotes: 0
Views: 2915
Reputation: 27497
I eventually did this:
extension WebBrowserViewController: WKNavigationDelegate {
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
print(webView.URL?.absoluteString)
if urlVideoType(webView.URL!) != VideoUrl.Unknown {
loadVideo(webView.URL!)
decisionHandler(.Cancel) //<------------------ this part
}
decisionHandler(.Allow) //<------------------ this part
}
}
Upvotes: 7