how to open an URL in Swift3 how to open an URL in Swift3 ios ios

how to open an URL in Swift3


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)}


Above answer is correct but if you want to check you canOpenUrl or not try like this.

let url = URL(string: "http://www.facebook.com")!if UIApplication.shared.canOpenURL(url) {    UIApplication.shared.open(url, options: [:], completionHandler: nil)    //If you want handle the completion block than     UIApplication.shared.open(url, options: [:], completionHandler: { (success) in         print("Open url : \(success)")    })}

Note: If you do not want to handle completion you can also write like this.

UIApplication.shared.open(url, options: [:])

No need to write completionHandler as it contains default value nil, check apple documentation for more detail.


If you want to open inside the app itself instead of leaving the app you can import SafariServices and work it out.

import UIKitimport SafariServiceslet url = URL(string: "https://www.google.com")let vc = SFSafariViewController(url: url!)present(vc, animated: true, completion: nil)