How to Open an Url in Swift

How to open an URL in Swift?

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

Open web url link in browser in Swift

You need to check

like this

if let url = URL(string: urlSting.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}

Also try removing and/or adding https:// prefix,

and if does not work then simply do:

if let url = URL(string: urlSting), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}

With Swift, is there a way to open URL and get the http error

You can get your responses by doing something like this.

let URL = URL(string: "www.google.com")
let task = URLSession.shared.dataTask(with:url!) { _, response, _ in
if let status = response as? HTTPURLResponse {
print(status.statusCode)
}
}

task.resume()

Apologies for any syntax issues. No XCode available at the moment.

SwiftUI: How do I make a button open a URL in safari?


iOS 14.0+

Using Link:

Link("Some label", destination: URL(string: "https://www.mylink.com")!)

Older versions

Button(action: {
if let url = URL(string: "https://www.mylink.com") {
UIApplication.shared.open(url)
}
}) {
Text("Some label")
}

iOS, Swift 5, AppDelegate open url / .onOpenUrl - how to find calling app that calls my custom scheme

So I managed to use this in the app delegate:

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
sceneConfig.delegateClass = SceneDelegate.self
return sceneConfig
}

Which sets this up in the scenedelegate:

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
// get url and sourceApp out of first URLContext here
}

Unfortunately, if the sender of the URL is NOT from your team, you'll get nil in the UIOpenURLContext.sourceApp

So if you're wanting to create RPCs between your teams apps, you're fine.
If you want to communicate between apps from different teams, you'll have to figure out some other way to indicate which app it is and then apply your responding policy to that.

Swift Open Link in Safari

It's not "baked in to Swift", but you can use standard UIKit methods to do it. Take a look at UIApplication's openUrl(_:) (deprecated) and open(_:options:completionHandler:).

Swift 4 + Swift 5 (iOS 10 and above)

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.open(url)

Swift 3 (iOS 9 and below)

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.openURL(url)

Swift 2.2

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.sharedApplication().openURL(url)


Related Topics



Leave a reply



Submit