0

I am opening links in Webviewcontroller. I have created a button on webview controller to open that link in Safari Browser. Problem I am facing is when I open url before it get load completely in web view controller, it open in Safari browser successfully. But when URL loaded completely in webview controller, it doesn't open Safari Broswer.

My Code is:

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
    alertController.addAction(UIAlertAction(title: NSLocalizedString("Open in Safari", comment: ""), style: .default, handler: { (UIAlertAction) -> Void in
        self.openInSafari()
    }))

    if  (UIDevice.current.userInterfaceIdiom == .phone){
        alertController.addAction(UIAlertAction(title:  NSLocalizedString("Cancel",comment: ""), style: .cancel, handler:nil))
    }

And Function to open Safari Browser:

func openInSafari(){

    if #available(iOS 10.0, *) {
        UIApplication.shared.open(URL(string: self.currentUrl)!, options: [:],completionHandler: nil)
    } else {
        UIApplication.shared.openURL(URL(string: self.currentUrl)!)
    }

}

Screenshots:

Screenshot 1 Screenshot 2

rohit phogat
  • 137
  • 1
  • 10

1 Answers1

0

Please try this once:

guard let url = URL(string: self.currentUrl) else {
    return //be safe
}

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(url)
}
maxwell
  • 3,788
  • 6
  • 26
  • 40
Vineesha V
  • 11
  • 1
  • 6