Last active
September 12, 2023 09:23
-
-
Save imjhk03/4b42445706f008ca772a01e56ce515d3 to your computer and use it in GitHub Desktop.
Open a URL in Safari or App (Helper)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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