Swift
iOS Development
URL Handling
Programming Tutorial
SwiftUI

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.

swift
1import UIKit
2
3if let url = URL(string: "https://www.example.com") {
4    UIApplication.shared.open(url, options: [:], completionHandler: nil)
5}

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.

swift
1import SwiftUI
2
3struct ContentView: View {
4    @Environment(\.openURL) private var openURL
5
6    var body: some View {
7        Button("Open Website") {
8            guard let url = URL(string: "https://www.example.com") else { return }
9            openURL(url)
10        }
11    }
12}

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.

swift
1import UIKit
2
3if let url = URL(string: "tel:1234567890"),
4   UIApplication.shared.canOpenURL(url) {
5    UIApplication.shared.open(url)
6}

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.

swift
1import SafariServices
2import UIKit
3
4final class ViewController: UIViewController {
5    func showPage() {
6        guard let url = URL(string: "https://www.example.com") else { return }
7        let controller = SFSafariViewController(url: url)
8        present(controller, animated: true)
9    }
10}

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:

  • 'https opens web content'
  • 'mailto opens a mail app compose flow'
  • 'tel opens 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.open in UIKit to open URLs with the system.
  • Use SwiftUI's openURL environment action in SwiftUI views.
  • Validate URL construction before trying to open anything.
  • Use canOpenURL when scheme support matters.
  • Use SFSafariViewController when web content should stay inside the app.

Course illustration
Course illustration

All Rights Reserved.