With iOS 9 Apple has introduced an exciting new way to interact with the web inside apps: SFSafariViewController. Unlike its counterpart WKWebView, SFSafariViewController provides a fully featured user interface right out of the box with tons of great features that benefit both users and developers. Let's look at six of those benefits.

1. Familiar Safari Interface

SFSafariViewController provides users with a familiar interface that looks similar to the Safari app itself. At the top is a web address text field which collapses while scrolling. At the bottom is the familiar backward and forward navigation buttons, and a share button. There is also a new button that looks like the Safari icon. Tapping this button opens the full Safari app with the current website.

Note: The web address text field is not editable! Users can navigate by tapping links and using navigation buttons, but they cannot enter URLs manually.

2. Share Button

From the share button on the bottom navigation bar users have access to all of their favorite app extensions. Apps that present a SFSafariViewController also have the opportunity to add their own actions to the share sheet using a delegate method.

3. Reader View

The Reader view from Safari is also available in SFSafariViewController for reading long form content. Reader theme preferences are shared with Safari.

4. Security Icon and Phishing Detection

SFSafariViewController presents a lock icon when loading pages securely using https. It also presents a warning when loading pages that are suspected to be phishing. This gives the user greater awareness and confidence in the content they are viewing.

5. Content Blocking

Content blocking, a feature new to iOS 9, works in SFSafariViewController just like it does in Safari. Content blockers provide users with options to avoid tracking software, explicit content, advertisements, and more. This leads to faster and more secure browsing.

6. AutoFill and Cookies

SFSafariViewController allows users to AutoFill sensitive information and credentials securely from their iCloud keychain. This includes filling in passwords, credit card numbers, and contact information. This provides a level of security and convenience that just wasn't possible in apps before. Furthermore, cookies are shared between Safari and SFSafariViewController which means that user sessions and preferences persist between the two. Authorizing an app to access Twitter or Facebook, for example, will be a streamlined experience if the user is already signed in with Safari or if their passwords are stored in iCloud Keychain.

How do I use it?

The SFSafariViewController API is delightfully simple. To create a SFSafariViewController just pass in a URL:

let bookmark = NSURL(string: "http://www.apple.com")!
let safari = SFSafariViewController(URL: bookmark)
presentViewController(safari, animated: true, completion: nil)

To open SFSafariViewController with Reader open use the full designated initializer:

let safari = SFSafariViewController(URL: bookmark, entersReaderIfAvailable: true)

When SFSafariViewController is presented it appears fullscreen with a ‘Done' button at the top right. However, tapping this button does nothing unless the SFSafariViewController instance has a delegate. The SFSafariViewControllerDelegate protocol offers three optional methods. To respond to the ‘Done' button a delegate must implement safariViewControllerDidFinish:.

func safariViewControllerDidFinish(controller: SFSafariViewController) {
 controller.dismissViewControllerAnimated(true, completion: nil)

While this method is technically optional, an app that does not dismiss the view controller when the user taps done might feel broken to the user. Next is safariViewController:didCompleteInitialLoad:. This method lets the delegate know that loading has completed and indicates whether or not that load was successful.

func safariViewController(controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
 if didLoadSuccessfully == false {
 print("Page did not load!")
 controller.dismissViewControllerAnimated(true, completion: nil)

The last delegate method is safariViewController:activityItemsForURL:title:. This method asks the delegate to provide an array of UIActivity items that will be available to the user in the Actions row of the share sheet. Learn more about UIActivity on the iOS Developer Library.

What's next?

What SFSafariViewController lacks in terms of the flexibility and customization of WKWebKit it makes up for in terms of security and convenience for users and developers alike. Download the sample project linked below to see SFSafariViewController in action and consider using it in your own apps.