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.
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.
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.
Click Next, and we will be prompted to provide the app info as shown below.
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.
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.
Now, click the Open Web View button, and we will notice that the safari view controller will be presented below.
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.