WWDC Notes

Discover WKWebView enhancements

Show Apple's description.
WKWebView is the best way to present rich, interactive web content right within your app. Explore new APIs that help you convert apps using WebViews or UIWebViews while adding entirely new capabilities. Learn about better ways to handle JavaScript, fine tune the rendering process, export web content, and more.

iOS in-app browser choices

In iOS we have mainly two ways: SFSafariViewController and WKWebView.

SFSafariViewController

  • If we don't need deep customization, SFSafariViewController is the best choice
  • Built on top of WKWebView
  • It handles everything for you
  • Provides many Safari features such as:
    • reader
    • content blockers
    • autofill
    • and more

WKWebView

  • More customizable
  • Protects your application's code and data from the complexities of the web platform
  • The web content runs in a separate process

What's new in WKWebView

Isolating your app from web content

Communicating with JavaScript

  • callAsyncJavaScript makes reusing the same script with different values much more like native functions:
  • With callAsyncJavaScript serialization and de-serialization of argument types happens automatically
  • The callAsyncJavaScript completion block is called only after the script code says so: it the script returns a promise, it will call the completion block only after the promise has been fulfilled.

More flexible rendering

  • Use WKWebView.pageZoom instead of changing the CSS zoom with JavaScript. This will make sure that the zoom is set before the page is rendered.
  • Use WKWebView.mediaType to set a custom media type (used in css queries)

Working with web content

Respecting privacy

  • Intelligent Tracking Prevention (ITP) is enabled by default on all WKWebViews on iOS 14 and macOS 11

App-bound domains

  • you specify which domains are the core part of the implementation of your app
  • deep interaction with the web content not core to your app is disabled, making it impossible for user data to be accidentally compromised by other domains

Define your domains in the app info.plist:

Missing anything? Corrections? Contributions are welcome 😃

Related

Written by

zntfdr

Federico Zanetello

Federico Zanetello is an iOS Engineer with strong passion for Swift, minimalism, and design. When he’s not busy automating things, he can be found writing at fivestars.blog and/or playing with the latest shiny toys.