SFSafariViewController improvements in iOS 15
There's an option to run custom extension on the page content and also preload some content.
Published: June 12, 2021 Sponsored I want a Press KitLet's look at two new additions to SFSafariViewController
. As a reminder this is a class that lets us display a webpage inside our app with familiar look to the Safari browser. It is way easier to use than full-fledged WKWebView
.
Pre-loading content
One of the new options lets us start loading pages before the SFSafariViewController
is opened. This might be useful in cases when you need it to load resource-intensive webpages and want to provide better experience to the user.
To do this, there is class-level method prewarmConnections
which takes as its input an array of URL
s to "prewarm". You will get back a token so you can later cancel this operation. Maybe user opens a screen that lets them open the website, but they change their mind, so in the viewDidDisappear
you could invalidate this token using its invalidate
method.
let urls = [URL(string: "https://apple.com")!]
let token = SFSafariViewController.prewarmConnections(to: urls)
Running custom extension
The second new feature is the ability to run custom extension in the SFSafariViewController
. We can use the new configuration options to specify bundle identifier for the extension and image which will be used for the button the controller will display.
let config = SFSafariViewController.Configuration()
config.activityButton = .init(templateImage: UIImage(systemName: "sun.max")!, extensionIdentifier: "com.bundle.extension")
let safariVC = SFSafariViewController(url: URL(string: "https://apple.com")!, configuration: config)
Note that this has to be Share Extension according to the WWDC session. I cannot think of an use case but since Apple added this, I guess there are valid ones.
Let me know if you find this particularly useful - I am curious.
Uses: Xcode 13 & Swift 5.5