0

I am developing an application using UIWebView. Inside the web view I have mailto link and I want to open this link in the mail application of iOS.

That is my entire code of the application

 override func viewDidLoad() {
    //webview.delegate = self;
    webview.dataDetectorTypes = UIDataDetectorTypes.all
    super.viewDidLoad()
    self.webview.delegate = self
     let webURL = URL(string: "http://www.collegiodeirettori.net/palio/")
    // Do any additional setup after loading the view, typically from a nib.



    let urlRequest = URLRequest(url: webURL!)

    webview.loadRequest(urlRequest)
    webview.scrollView.bounces = false




}

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {


    print(request)
    let stringaURL = try! String(contentsOf: request.url!, encoding: String.Encoding.ascii)

    if navigationType == UIWebViewNavigationType.linkClicked{
        if((stringaURL.range(of:"www.collegiodeirettori.net/palio/")) != nil ){
            print("ciao")
            //print(stringaURL)
        }
        else{
                print(stringaURL)
            UIApplication.shared.openURL(request.url!)
            //UIApplication.shared.open(URL(string: stringaURL)!)
            return false;
        }


}


    return true;
}

I already made a func that control if a link have to be opened in safari and that works but no for the mailto link.. Any ideas? Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
alex
  • 380
  • 3
  • 8
  • are you trying to open this in the simulator or on the device? – Michael Dautermann Mar 12 '17 at 20:40
  • Both of them and It doesn't work – alex Mar 13 '17 at 17:06
  • @Alessandro, does your code actually get executed? Do you see in log your `print(stringaURL)` under debugger? `openURL` returns BOOL, could you log it as well? Also have you considered using [`MFMailComposeViewController`](https://developer.apple.com/reference/messageui/mfmailcomposeviewcontroller)? Also does your device have at least one e-mail account fully set up? See https://developer.apple.com/reference/messageui/mfmailcomposeviewcontroller/1616879-cansendmail and http://stackoverflow.com/questions/2481029/when-will-mfmailcomposeviewcontroller-cansendmail-return-no – SergGr Mar 14 '17 at 03:16
  • Thanks for the reply. Well I see in log print(stringaURL). I tried to execute the program under my iPad Mini and on the device I have a mail account which is configured with the Mail's app of iOS. – alex Mar 16 '17 at 15:25
  • The error that appeared when I try to open a mail to link is Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened." UserInfo={NSURL=mailto:biglietteriapalio@comune.asti.it} – alex Mar 16 '17 at 15:48

0 Answers0