Reputation: 131
I can't find solution for this. I hope You'll help me. I want buttons with hyperlinks to open websites in webview on second screen. Hyperlink opens in safari and I want it to open on second screen in webview. Thank you
Upvotes: 12
Views: 12989
Reputation: 183
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void){
if let url = navigationAction.request.url, let scheme = url.scheme?.lowercased() {
if scheme == "https" || scheme == "http"{
if UIApplication.shared.canOpenURL(url){
// use the available apps in user's phone
UIApplication.shared.open(url)
}
}
}
decisionHandler(.allow)
}
Upvotes: 0
Reputation: 10329
Swift 4.1 and Swift 5. If there are some links inside the app then it will open in safari, So to opens the links within same webview implement following navigationDelegate fo WKWebView.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == WKNavigationType.linkActivated {
webView.load(navigationAction.request)
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
Upvotes: 7
Reputation: 11127
You can use SFSafariViewController to open link in your application, here is the code for that
First you need to import SafariServices
import SafariServices
then on the button action, you can open SFSafariViewController
For swift 3.0
let svc = SFSafariViewController(url: URL(string:"http://stackoverflow.com")!)
self.present(svc, animated: true, completion: nil)
Upvotes: 20