How to open a URL in Swift?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Opening a URL in Swift can mean either handing the link to the system or showing the destination inside your app. The correct API depends on whether you are writing UIKit or SwiftUI code, and whether the URL is a standard web link, a phone or mail link, or a custom app scheme.
Open a URL in UIKit
In a UIKit app, the standard approach is UIApplication.shared.open.
This asks the system to open the URL using the appropriate app. For an https link that is usually Safari, unless the link is configured as a universal link and another app handles it.
The key detail is that URL(string:) can fail, so the URL should always be created safely instead of force-unwrapped.
Use SwiftUI's openURL
In SwiftUI, use the environment action instead of reaching directly for UIApplication from the view.
This is the idiomatic solution for SwiftUI because it keeps the action in the view layer and works naturally with the framework's environment model.
Validate and Check Before Opening
For custom schemes such as tel, mailto, or another app's deep link, it is often useful to check whether the system can open the URL first.
This is especially important for non-web URLs, where support depends on device capabilities and app configuration.
If you only need to open a normal website, canOpenURL is often unnecessary. The bigger concern there is simply validating that the string became a real URL.
Use SFSafariViewController for In-App Browsing
If you want the user to stay inside your app while viewing a webpage, use SFSafariViewController instead of launching Safari.
This gives you a secure, system-provided browser experience without building your own web view plumbing. It is usually a better choice than dropping straight to WKWebView for simple article or help-page display.
Understand Scheme-Specific Behavior
Not every URL is just a webpage. These examples behave differently:
- '
httpsopens web content' - '
mailtoopens a mail app compose flow' - '
telopens the phone flow on supported devices' - custom schemes can deep-link into other apps
That means opening a URL is partly a user-experience decision. A help article might belong in SFSafariViewController, while an external partner site may be better opened in Safari so the user can navigate more freely.
If you use custom schemes, remember that app-level queries may require configuration in your app metadata before canOpenURL works as expected.
Common Pitfalls
The most common mistake is treating every string as a valid URL. If URL(string:) returns nil, nothing should be opened.
Another issue is using UIApplication.shared.open inside SwiftUI views when openURL is a cleaner framework-native choice.
Developers also sometimes use an in-app web view for content that should really leave the app, or they open Safari when the better experience would be to keep the user inside the current flow with SFSafariViewController.
Finally, canOpenURL is not a magic validator for every case. It is most useful for custom or device-specific schemes, not as a substitute for proper URL construction.
Summary
- Use
UIApplication.shared.openin UIKit to open URLs with the system. - Use SwiftUI's
openURLenvironment action in SwiftUI views. - Validate URL construction before trying to open anything.
- Use
canOpenURLwhen scheme support matters. - Use
SFSafariViewControllerwhen web content should stay inside the app.

