5

I have build a Swift iOS app with the only purpose to display a website with the help of WKWebView.

This works fine, but pressing on links (e.g. on an [mailto:] button) doesn't work because it can't open anything!

Does anybody have a solution for this?

I have read a lot about solving this, but I don't know where to start.

Thanks for the help

[UPDATE: ]

The code below shows the solution

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

    @IBOutlet var containerView : UIView! = nil
    var webView: WKWebView?

    override func loadView() {
        super.loadView()

        self.webView = WKWebView()
        self.view = self.webView!
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        webView?.navigationDelegate = self
        let url = NSURL(string:"https://google.de")
        let req = NSURLRequest (URL: url!)
        self.webView!.loadRequest(req)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {

        let url = navigationAction.request.URL?.absoluteString
        let url_elements = url!.componentsSeparatedByString(":")

        switch url_elements[0] {
        case "mailto":
            openCustomApp("mailto://", additional_info: url_elements[1])
            decisionHandler(.Cancel)

        default:

        }

        decisionHandler(.Allow)
    }

    func openCustomApp(urlScheme:String, additional_info:String){

        if let requestUrl:NSURL = NSURL(string:"\(urlScheme)"+"\(additional_info)") {
            let application:UIApplication = UIApplication.sharedApplication()
            if application.canOpenURL(requestUrl) {
                application.openURL(requestUrl)
            }
        }
    }
}
OYPS
  • 81
  • 1
  • 2
  • 13
  • 1
    Looks fine. Did you debug the decidePolicyForNavigationAction ? The url for example ? Basically you just need mailto://tes@test.com – OhadM Sep 27 '16 at 14:23
  • you just solved the problem. The answer below is the right way to do it and the code above shows the solutions. My only problem was that the mailto: link wasn't formatted the right way! thanks to you all – OYPS Sep 27 '16 at 14:37

1 Answers1

7

You have to implement the following method (part of WebKit), which can be used to parse such URLs:

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void)

The idea is to write if-statements to check for certain URL types before passing the decision. More info here:

Keep in mind that some of the syntax for delegates has changed slightly in Swift 3 - something to keep in mind when using tutorials. If you have already found this but you ran into an error - post your code.

EDIT: just an update for others who stumble upon this question. Keep in mind that you need the right formatting for mailto e.g. mailto://tes@test.com (as mentioned by OhadM in the comments of the OP)

Community
  • 1
  • 1
tech4242
  • 2,348
  • 2
  • 23
  • 33
  • Thanks for the help so far, i found the GitHub example you posted but i am not sure how and where to put it and how to use it – OYPS Sep 27 '16 at 12:04
  • @ChrisNeumaier You have to use it in the ViewController where you use WebKit. Start typing it within your class and Xcode will help you with the code skeleton. The method is already there as a part of WebKit - like viewDidLoad is a part of UIKit. decidePolicyForNavigationAction is there to help you decide what to do with URLs – tech4242 Sep 27 '16 at 12:05
  • i updated my original post with my ViewController, i played with the function you suggested but i can't get it to work properly – OYPS Sep 27 '16 at 12:24
  • @ChrisNeumaier You should at least put your implementation in the code you posted. You won't get it to work without the function and I can't see what you are doing wrong if you don't show us – tech4242 Sep 27 '16 at 13:23
  • You are right. I updated the code block above. The website still opens properly but nothing happens if i click on a mail link [mailto:]. I just wanted to try it with the sample code to understand the concept – OYPS Sep 27 '16 at 13:51
  • 1
    @ChrisNeumaier I am glad you figured it out - I saw your other comment beneath your post. I will update my answer with the formatting tip (just in case somebody stumbles upon this) – tech4242 Sep 27 '16 at 14:38