I want a button inside my app, that will open safari and a specific URL address. How do I do this? UIApplication.sharedApplication doesn't seem to work in SwiftUI.
This is my first question here. Please give feedback if I did something wrong 😸
Using Link:
Link("Some label", destination: URL(string: "https://www.mylink.com")!)
Button(action: {
if let url = URL(string: "https://www.mylink.com") {
UIApplication.shared.open(url)
}
}) {
Text("Some label")
}
If you are using a SceneDelegate you can handle URL addresses using the following:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
//Handle URL here
}
From there you can call openURL like you normally would:
UIApplication.shared.open(url, options: [:], completionHandler: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With