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
Uigesturerecognizer on Uiimageview
Prevent Deploying (Disable) Watchkit App with iOS iPhone App in Xcode
Iphone: Where the .Dsym File Is Located in Crash Report
Resizing a Uilabel to Accommodate Insets
iOS Nsdate() Returns Incorrect Time
Filemanager Cannot Find Audio File
Error: Initializer for Conditional Binding Must Have Optional Type, Not 'String'
Create a Rectangle with Just Two Rounded Corners in Swift
How to Add a 'Done' Button to Numpad Keyboard in iOS
How to I Import 3Rd Party Frameworks into Xcode Playground
iOS 8 Swift - Tableview with Embedded Collectionview
How to Access My Viewcontroller from My Appdelegate? iOS
Swift Default Alertviewcontroller Breaking Constraints
Uilocalnotification Is Deprecated in iOS 10
Add Entry to iOS .Plist File via Cordova Config.Xml
Formatting a Uitextfield for Credit Card Input Like (Xxxx Xxxx Xxxx Xxxx)
How to Change My iOS Applications' Entitlements
How to List Out All the Subviews in a Uiviewcontroller in iOS