WKWebView open links from certain domain in safari
Here is sample code from the response to the swift written in obj c.
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler
{
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
if (navigationAction.request.URL) {
NSLog(@"%@", navigationAction.request.URL.host);
if (![navigationAction.request.URL.resourceSpecifier containsString:@"ex path"]) {
if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
decisionHandler(WKNavigationActionPolicyCancel);
}
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
You can implement WKNavigationDelegate
, add the decidePolicyForNavigationAction
method and check there the navigationType and requested url. I have used google.com below but you can just change it to your domain:
Xcode 8.3 • Swift 3.1 or later
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.frame = view.bounds
webView.navigationDelegate = self
let url = URL(string: "https://www.google.com")!
let urlRequest = URLRequest(url: url)
webView.load(urlRequest)
webView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
view.addSubview(webView)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
if let url = navigationAction.request.url,
let host = url.host, !host.hasPrefix("www.google.com"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
print(url)
print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
return
} else {
print("Open it locally")
decisionHandler(.allow)
return
}
} else {
print("not a user click")
decisionHandler(.allow)
return
}
}
}
Swift 4 update for George Vardikos answer:
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
let url = navigationAction.request.url
guard url != nil else {
decisionHandler(.allow)
return
}
if url!.description.lowercased().starts(with: "http://") ||
url!.description.lowercased().starts(with: "https://") {
decisionHandler(.cancel)
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
decisionHandler(.allow)
}
}
For Swift 3.0
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
let wv = WKWebView(frame: UIScreen.main.bounds)
override func viewDidLoad() {
super.viewDidLoad()
guard let url = NSURL(string: "https://www.google.com") else { return }
wv.navigationDelegate = self
wv.load(NSURLRequest(url: url as URL) as URLRequest)
view.addSubview(wv)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .LinkActivated {
if let newURL = navigationAction.request.url,
let host = newURL.host , !host.hasPrefix("www.google.com") &&
UIApplication.shared.canOpenURL(newURL) &&
UIApplication.shared.openURL(newURL) {
print(newURL)
print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
} else {
print("Open it locally")
decisionHandler(.allow)
}
} else {
print("not a user click")
decisionHandler(.allow)
}
}
}