Javatpoint Logo
Javatpoint Logo

SFSafariViewController in iOS

SFSafariViewController is an object used to provide safari features in the iOS app like Reader, Autofill, Fraudulent Website Detection, and content blocking. Using SFSafariViewController, we can build a standard visible interface for browsing the web in our iOS app.

The SFSafariViewController is a class that inherits ViewController in UIKit. The syntax to use SFSafariViewController is given below.

The SFSafariViewController doesn't share the user's interaction or activity with the application; therefore, the app can't access the auto-fill data, browsing history, or website data. We don't need to secure data between our app and safari. However, if we want to share the data between our app and safari, the user can log in using ASWebAuthenticationSession in iOS 11 and later.

While using SFSafariViewController, we must notice that according to App Store Review Guidelines, this view controller should be used to present information to users; the controller may not be hidden by the other views or layers. Also, we should not use SFSafariViewController to track the activities of the users without their consent.

The UI features in an SFSafariViewController include the following points.

  1. A read-only address field is provided with a security indicator and a Reader button.
  2. It contains an Action button that is used to invoke an activity view controller that offers the custom services in our app.
  3. it provides a
  4. Done button, back, and forward navigation buttons, and a button to open the page directly in Safari
  5. On devices that support 3D Touch, automatic Peek and Pop for links and detected data.

In this tutorial, we will create an iOS app that uses SFSafariViewController to present the web view. We will also discuss why we should use SFSafariViewController to provide a web browsing experience to the users.

First, let's discuss the Methods and properties of SFSafariViewController class.


The SFSafariViewController class includes the following properties.

SN Property Description
1 var delegate: SFSafariViewControllerDelegate? It is an object of the SFSafariViewControllerDelegate protocol that provides functions that implements the behaviors for the Safari view controller's Done and Action buttons.
2 var configuration: SFSafariViewController.Configuration It represents the copy of the Safari ViewControllers's initialized configuration.
3 var dismissButtonStyle: SFSafariViewController.DismissButtonStyle It represents the dismiss button style for the safari view controller.
4 var preferredBarTintColor: UIColor? It is the UIColor object which represents the color to tint the background of the navigation bar and the toolbar.
5 var preferredControlTintColor: UIColor? It is the UIColor object which represents the color to tint the control buttons on the navigation and toolbar.


Let's create a demo iOS app that uses an SFSafariViewController to provide a web browsing experience.

Let's create the XCode project as shown below.

SFSafariViewController in iOS

Click Next, and we will be prompted to provide the app info as shown below.

SFSafariViewController in iOS

Let's create the interface for the view controller. Our view controller provides a UIButton clicking which, we will be navigated to Safari View Controller.

Our View Controller includes the following interface.

SFSafariViewController in iOS

Let's create the action outlet for the "Open Web View" button as openWebView

and add the following code.

To use SFSafariViewController in the app, we need to import the SafariServices in the ViewController. To import SafariServices, add the following line of code in the ViewController.swift.

We also need our ViewController to conform to the SFSafariViewControllerDelegate and provide delegate methods implementation as shown below.

Now, let's run the app in the simulator.

SFSafariViewController in iOS

Now, click the Open Web View button, and we will notice that the safari view controller will be presented below.

SFSafariViewController in iOS

If we click on the top left corner of the screen, the Safari View Controller will be dismissed.

The ViewController.swift will contain the following code.

When should we use SFSafariViewController in our app?

We can use SFSafariViewController in the scenarios where we want to display websites in our iOS app without sending users to safari. By using SFSafariViewController, we embed the safari interface. We can also use many of the key features and privacy protections of Safari in our app. We should use SFSafariViewController when we need to display interactive web experiences on websites we don't own or showcase parts of your web content that are generally outside the scope of your app.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA