handling external links in swift UIWebKit?
handling external links in swift UIWebKit?
I'm working on a webview using swift 4.
I'm loading local html files, in these pages are some links to other websites, but once I click on any of them I want to load them in safari or default browser instead of the WebView (browser).
my webView is called "browser"
Here are my ViewController.swift codes:
import UIKit
import WebKit
class ViewController: UIViewController
@IBOutlet weak var browser: WKWebView!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let htmlpath = Bundle.main.path(forResource: "kusrc/index", ofType: "html")
let url = URL(fileURLWithPath: htmlpath!)
let request = URLRequest(url: url)
browser.load(request)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
Could somebody help me out? most of the results in google were not what I needed...
Is there the possibility to make an if sentence, where it gets asked if the stringprefix ="http:// or https://" then open safari, otherwise "browser"
1 Answer
1
I think you are looking for this delegate method:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
if navigationAction.navigationType == .linkActivated
guard let url = navigationAction.request.url else
decisionHandler(.allow)
return
UIApplication.shared.open(url)
decisionHandler(.cancel)
else
decisionHandler(.allow)
If you want to differentiate according the URL scheme, you can use URLComponents to split the URL in its parts.
URL
URLComponents
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
if components?.scheme == "http" || components?.scheme == "https"
// insert your code here
// edit (a bit more detailed):
WebKit
import WebKit
WKNavigationDelegate
class ViewController: UIViewController, WKNavigationDelegate
browser.navigationDelegate = self
A gist of how your class should look like in the end: WKWebView open links in Safari
And here is a very nice tutorial about the topic: https://www.hackingwithswift.com/example-code/wkwebview/how-to-control-the-sites-a-wkwebview-can-visit-using-wknavigationdelegate
No worries, i edited my answer.
– palme
Aug 25 at 19:49
unfortunately it still didn't work :/
– Tareq Jami
Aug 25 at 20:07
Can you explain what exactly does not work and maybe share the URL you are handling?
– palme
Aug 25 at 20:10
I copy pasted these codes gist.github.com/kevenbauke/d449718a5f268ee843f286db88f137cc
– Tareq Jami
Aug 25 at 20:12
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I must to be honest, I'm actually developing android apps and it's my first time to develope on swift. sorry for this dumb question, but where shall I add all this?
– Tareq Jami
Aug 25 at 19:25