Should I use WKWebView or SFSafariViewController for web views in my app?
July 29, 2020
Whether your app needs to provide a full web browsing experience, display richly-styled content, or incorporate external websites without taking people out of your app, you can make the experience smooth and seamless by choosing the right API.
You can display web content inside of your app with both the WKWebView and SFSafariViewController APIs. But which is the best for your app’s needs?
Note: If your app uses the deprecated UIWebView API to display web content, you will need to update your code as soon as possible for improved security, performance, and reliability. Learn more:
SFSafariViewController is part of the SafariServices framework, and lets your users browse a web page, or a website right inside your app. With it, people can enjoy the same web browsing experience they get in Safari — including features like Password Autofill, Reader, and Secure Browsing — without ever having to leave your app.
These two APIs can provide a lot of the heavy lifting for web technologies in your app, though there are a few instances where we recommend alternative frameworks. For example, when presenting a web-based login screen for your app, use ASWebAuthenticationSession to provide people with the most secure experience.
When should I use WKWebView?
If you need to customize or control the display of web content — or interact with the content itself — WKWebView will be most flexible in helping you build the implementation that suits your needs. (If your app is designed to be used offline, make sure any WKWebView content has appropriate fallbacks and alerts.)
Additionally, consider WKWebView if you need to display HTML or CSS content inline or as part of rest of your app’s user interface.
In short, WKWebView is an incredibly powerful technology that works in tandem with iOS and macOS frameworks. That said, WKWebView is not designed to outright replace system technologies and frameworks. For example, you should avoid using it in place of device-optimized UIKit classes like UITableView, UIImage, and UIButton, as you lose out on core system behaviors and provide a subpar experience for people who use your app.
When should I use SFSafariViewController?
When you want display websites inside your app without sending people to Safari, the best tool is SFSafariViewController. By using this API, you can effectively embed the Safari interface — and many of its key features and privacy protections — into your app.
SFSafariViewController is best used when you need to display interactive web experiences on websites you don’t own, or showcase parts of your web content that are generally outside the scope of your app.