WKWebView open links from certain domain in safari
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
}
}
}
WKWebView Open _Bank Target Links in Safari
This is what eventually worked for me. Seems to open any url (including javascript type popovers used in YouTube) in Safari instead of opening them in WKWebView (or not opening them at all).
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
NSLog(@"createWebViewWithConfiguration %@ %@", navigationAction, windowFeatures);
if (!navigationAction.targetFrame.isMainFrame) {
[[NSWorkspace sharedWorkspace] openURL:[navigationAction.request URL]];
}
return nil;
}
Unable to open external link in safari using WKWebView
i was missing the order
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false
webViewContainer.addSubview(webView)
[webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor),
webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor),
webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor),
webView.rightAnchor.constraint(equalTo:
webViewContainer.rightAnchor)].forEach { anchor in
anchor.isActive = true
}
self.webView.uiDelegate = self
self.webView.navigationDelegate = self
Related Topics
Exc_Bad_Access When Building Nspredicate
What Is Export * in Module.Modulemap File Inside Each Framework
iOS Aes Encryption - Fail to Encrypt
Swift - Saving Highscore Using Nsuserdefaults
Force Landscape Viewcontroller in iOS 7
Trigger Local Notifications Automatically Daily on Dynamic Time Given in Arrays Objective C iOS
Optional Chaining in Swift Closure Where Return Type Has to Be Void
How to Get iOS Device MAC Address Programmatically
Table View Images Never Being Released
Xcode Canvas for Swiftui Previews Does Not Show Up
Nstimer Timerwithtimeinterval: Not Working
Generate Random Numbers with a Given Distribution
Sharing Code Between Original iOS App and App Extension
Uinavigationcontroller "Back Button" Custom Text
How to Implement Auto-Complete for Address Using Apple Map Kit
Instead of Push Segue How to Replace View Controller (Or Remove from Navigation Stack)
How to Add Images for Different Screen Size from Assets.Xcassets in Xcode 8