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





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





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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

Node.js puppeteer - Use values from array in a loop to cycle through pages