How to Open a URL in the Default Browser (Swift)

In this tutorial, we will explain how to open a URL in the default browser using Swift.

This time, we'll create a simple iOS app where tapping the OPEN URL button opens the specified URL (https://www.swift.org/) in the default browser.

How to Open a URL in the Default Browser with Swift 1

Prepare an iOS App to Open a URL

First, let's create a simple iOS app that opens a URL in the default browser when a button is tapped.

In Xcode, create a new project under [iOS] > [App].

On the Main storyboard, add a Button to the View Controller. The design and placement can be arbitrary.

How to Open a URL in the Default Browser with Swift 2


Create a TouchUpInside action from the button named openURLTapped.

How to Open a URL in the Default Browser with Swift 3

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func openURLTapped(_ sender: Any) {
    }
}

How to Open a URL in the Default Browser from an iOS App with Swift

Now let's write the Swift code to open a URL in the default browser.

In iOS, you can use the open() method of UIApplication as shown below:

func open(_ url: URL, 
   options: [UIApplication.OpenExternalURLOptionsKey : Any] = [:], 
   completionHandler completion: ((Bool) -> Void)? = nil)

The first parameter url is required, while options and completionHandler are optional.

If you just want to open a URL in the browser, specifying only the url is enough.


Update the openURLTapped() method in ViewController.swift as follows:

@IBAction func openURLTapped(_ sender: Any) {
    guard let url = URL(string: "https://www.swift.org") else { return }
    UIApplication.shared.open(url)
}

On line 2, we create a URL object from the string. If the creation fails, the guard let statement exits early.

On line 3, we pass the generated url to UIApplication.shared.open(), which launches the default browser and opens the specified web page.


That's all you need to open a URL in the default browser from an iOS app using Swift.

You don't need to add any special permissions in info.plist.


It's rare for a device not to have a browser installed. However, if you want to check whether an app capable of handling the URL is installed, you can use UIApplication.shared.canOpenURL(url).


Test the App in the iOS Simulator

Let's install and run the app in the iOS simulator.


Select a simulator and run the app.

How to Open a URL in the Default Browser with Swift 4


When the app launches, you'll see a screen like this:

How to Open a URL in the Default Browser with Swift 5


Tapping the OPEN URL button launches the default browser (Safari by default) and opens https://www.swift.org.

How to Open a URL in the Default Browser with Swift 6


If you have Google Chrome set as the default browser, Chrome will launch instead and open https://www.swift.org.


That's it! You've learned how to open a URL in the default browser from an iOS app using Swift.