iosswiftswift3

How to open a URL in Swift?


openURL has been deprecated in Swift 3.

Can anyone provide some examples of how the replacement openURL:options:completionHandler: works when trying to open a url?


Solution

  • All you need is:

    guard let url = URL(string: "http://www.google.com") else {
      return //be safe
    }
    
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }