Using SFSafariViewController in your iOS app

While developing iOS apps it is very common to encounter a situation when you have to open an external link. It can come with two scenarios whether you want to open the external link inside the app or outside the app.

There are some different ways in which you can open an external link from your iOS app:

  1. Create a new view controller and embed a web view inside it, and use it to open your link.
  2. Simply open the link inside Safari browser on your device.
  3. If the link is for an app that is already installed on your device than opening the app on tapping of the link. (This is already handled by iOS, you just have to implement the method 2 and rest is handled automatically)
  4. Open the link inside Safari which is inside your app. (SFSafariViewController)

Out of all the above methods I find the last method to be the best as it gives us many different options that makes it easier for the user to handle the link and save and share it also.

Additional things you get on implementing SFSafariViewController

With SFSafariViewController, you can use nearly all of the benefits of viewing web content inside Safari without forcing users to leave your app.

The majority of apps just need to provide a generalized web viewing experience. This is the perfect scenario for the safari view controller.

Before SFSafariViewController, the browsing experience was inconsistent across different apps, which may confuse the user. Some interfaces may also lack the things users expect, such as a progress bar indicating how much of the page is loaded.

Further, you don’t have access to all of Safari’s features. This includes the reader view, the iCloud keychain for autofill capabilities, and more. If you wanted to have those features before iOS 9, you were forced to have the user leave your app entirely by opening the content inside Safari. The SFSafariViewController class solves every one of these problems.

Implementing SFSafariViewController

Implementing Safari view controller is very easy. You just have to write 3–4 lines of code and all of the other work is done automatically. Here is the piece of code that you have to write if you want to open Google in your iOS app using SFSafariViewController.

let urlString = "https://www.google.com"
if let url = URL(string: urlString) {
  let safariVC = SFSafariViewController(url: url, entersReaderIfAvailable: true)
  self.present(safariVC, animated: true, completion: nil)
}

We have simply created a URL using a url string. If let binding is used so that in case URL is not created it will not crash the app. You can implement the else case to handle what happens when iOS fails to convert the string into URL. After URL has been created we simply have to create an instance of SFSafariViewController and pass it the URL. We also have used an extra argument which will enable the reader view that safari has if it’s possible. This argument is optional and you can also omit it. After this we have simply presented the view controller to open the external link in the app.

My point of view on SFSafariViewController

From the day it was released I’ve started using Safari view controller everywhere in my apps just for a very simple reason that it does not force the user to exit the app for opening the link as well as for sharing the same links on different apps.

Did you find this article valuable?

Support Samarth Paboowal by becoming a sponsor. Any amount is appreciated!