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 is straightforward once you separate two cases: opening a normal web link and asking the system to handle a custom URL scheme. On iOS, the main API is UIApplication.shared.open, while SwiftUI offers the openURL environment action for view-driven code.
UIKit: The Standard Way
In a UIKit app, the common solution is to build a URL and pass it to UIApplication.shared.open.
This asks the system to open the resource asynchronously. For an https URL, that usually means Safari or the user's preferred browser.
If you want to know whether the request succeeded, use the completion handler:
The completion result tells you whether the system accepted the open request. It does not mean the remote website loaded successfully.
Always Validate the URL First
The most basic failure case is an invalid string. URL(string:) returns an optional for a reason.
Bad:
Better:
This matters especially when the URL is built from user input, configuration, or server data.
canOpenURL and Custom Schemes
For custom schemes such as myapp:// or tel:, you may want to check whether the system can handle the URL before opening it.
Apple's documentation makes an important distinction here: canOpenURL has extra restrictions for undeclared custom schemes. If your app uses canOpenURL on schemes outside the standard ones, those schemes must be listed in LSApplicationQueriesSchemes in Info.plist.
That requirement applies to canOpenURL, not to ordinary open calls for normal web URLs.
SwiftUI Approach
In SwiftUI, the view-friendly solution is the openURL environment value.
This keeps the action aligned with SwiftUI's environment system and avoids reaching down into UIKit from a view when you do not need to.
SwiftUI also provides the built-in Link view for simple tappable links:
Use Link when the UI is just a link. Use openURL when you need custom logic before opening.
Universal Links Versus Custom URL Schemes
A normal https link may behave as a universal link if the destination domain is associated with an installed app. In that case, the system may route the user into the app instead of a browser.
Custom schemes such as myapp://profile/42 are app-specific and require the target app to register the scheme.
As a rule:
- use
httpsand universal links for public links - use custom schemes for app-to-app integration when appropriate
Universal links generally provide a better fallback story because the link still works in a browser when no app is installed.
Opening Settings or Other System URLs
Some URLs target system actions rather than websites. For example, apps sometimes open their own settings page:
This is useful when the app needs the user to update permissions such as notifications or location access.
Common Pitfalls
The most common mistake is force-unwrapping invalid URL strings. If the string is malformed, the app crashes before it even tries to open anything.
Another issue is overusing canOpenURL for ordinary web links. For https URLs, you usually do not need that extra check.
Developers also get tripped up by LSApplicationQueriesSchemes. The restriction applies when using canOpenURL with custom schemes, not when opening a normal website.
Finally, do not assume the completion handler means the destination finished loading. It only reports whether the system accepted the request to open the resource.
Summary
- In UIKit, use
UIApplication.shared.opento open a URL. - Always validate the URL string before opening it.
- Use
canOpenURLmainly for custom schemes, and remember theLSApplicationQueriesSchemesrequirement. - In SwiftUI, prefer
@Environment(\\.openURL)orLink. - Use universal links for public app-aware links and custom schemes for explicit app-to-app routing.

