Skip to content

Instantly share code, notes, and snippets.

@imjhk03
Last active September 12, 2023 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imjhk03/4b42445706f008ca772a01e56ce515d3 to your computer and use it in GitHub Desktop.
Save imjhk03/4b42445706f008ca772a01e56ce515d3 to your computer and use it in GitHub Desktop.
Open a URL in Safari or App (Helper)
enum OpenURL {
enum Scheme {
case url(String)
case tel(String)
case app(String)
}
static func open(scheme: OpenURL.Scheme, completion: ((Bool) -> Void)? = nil) {
let application = UIApplication.shared
let url: URL
switch scheme {
case .tel(let phoneNumber):
guard let finalUrl = URL(string: "tel://\(phoneNumber)") else { return }
url = finalUrl
case .url(let urlString):
guard let finalUrl = URL(string: urlString) else { return }
url = finalUrl
case .app(let urlString):
guard let finalUrl = URL(string: urlString) else { return }
if application.canOpenURL(finalUrl) {
application.open(finalUrl)
} else {
completion?(false)
}
return
}
application.open(url, options: [:], completionHandler: completion)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment