Swift How to make links open in Webview? Swift How to make links open in Webview? swift swift

Swift How to make links open in Webview?


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)


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)}