Use like
//inside scope use this
let myUrl = "http://www.google.com"
if let url = URL(string: "\(myUrl)"), !url.absoluteString.isEmpty {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
// or outside scope use this
guard let url = URL(string: "\(myUrl)"), !url.absoluteString.isEmpty else {
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
For more reference see this sample link.