Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WKWebView at Mobify

Avatar for Jason Voll Jason Voll
October 01, 2015

WKWebView at Mobify

A talk on WKWebView, why it's better (and worse) that UIWebView, and how we made it work for us at Mobify.

Avatar for Jason Voll

Jason Voll

October 01, 2015

Other Decks in Technology

Transcript

  1. What is a UIWebView? • Original web rendering component introduced

    in iOS 2.0 • This is what was historically used to build out hybrid aps or load any web content inside an iOS app
  2. UIWebView !== Safari • Mobile Safari tends to load pages

    2-3x quicker than a UIWebView • Changes and fixes applied to Safari usually not ported into UIWebView • Clunky, crashy, leaks memory • Scrolling blocks images loading (though we recently found a hack to fix this) • Different rendering and JavaScript engine than Safari (i. e. not Nitro)
  3. Why doesn’t UIWebView have Nitro? • Cynical answer: ◦ Nitro

    is excluded because Apple wanted to make hybrid apps less performant and force everybody to use their native platform • Real answer: ◦ Excluded due to security reasons ◦ Running a Just-In-Time (JIT) compiler requires marking pages in RAM as executable - iOS does not allow this out of security concerns (except in Safari)
  4. Introducing WKWebView • Introduced in iOS 8 to give developers

    a performant web view (“same” performance as Safari) • Key: WKWebView's rendering is all done in a separate process owned by the WKWebView
  5. What does WKWebView give us? • Significant performance gains in

    WebGL applications (though supported in UIWebview) • Less "crashy" behaviour when rendering bugs are triggered from HTML/CSS/JavaScript
  6. More benefits! • Faster JavaScript and page rendering (thanks Nitro!)

    • 60fps scrolling • Better communication between Swift and JS (we haven’t experimented with this yet) • Since we are sharing the JS engine with Safari, WKWebView should be better maintained and more reliable
  7. How do I implement it? • Don’t worry, there’s a

    tonne of great documentation! • What was formerly a single class (UIWebView) and protocol (UIWebViewDelegate) is now 14 classes and 3 protocols (WKNavigationDelegate, WKScriptMessageHandler, WKUIDelegate) • Other than class docs (and stackoverflow), no official “How-To WKWebView” from Apple
  8. WebKit Related Classes WKBackForwardList WKBackForwardListItem WKFrameInfo WKNavigation WKNavigationAction WKNavigationResponse WKPreferences

    WKProcessPool WKScriptMessage WKSecurityOrigin WKUserContentController WKUserScript WKWebViewConfiguration WKWebsiteDataRecord WKWebsiteDataStore WKWindowFeatures WKWebView
  9. More code required • Automatically handled by UIWebView ◦ App

    store links ◦ JS alerts, confirms, inputs ◦ Sharing cookies between web views
  10. Cannot load local file URLs • This is sorta critical

    for Astro… ◦ file: ///Users/jason/Library/Developer/CoreSimulator/Devic es/3AB02B9E-B5F4-4668-B9F3- 633F1D5053AD/data/Containers/Bundle/Application/E368 F75E-DD6B-44C2-B739-F8868BE0EED8/Astro%20Sandbox. app/www/index.html • UIWebView: ◦ webView!.loadRequest(request)
  11. Our Solution • High-level: copy the files we need to

    load into a /tmp directory that we can load files from • Alternative: copy files we need to a local webserver running on the device • Appears to be fixed this in iOS 9
  12. Cannot intercept POST requests • Previously did this to stack

    web views • POST bodies are missing in NSURLRequest sent to delegate methods • Solution: ◦ for now leave it -- in line with Android behavior ◦ future -- potential hack to let the post navigate, extract the result and put that in a new webview • Good news: WebKit forums indicate this is a bug and there seems to be a bit of traction
  13. UI state preservation/restoration broken • WKWebView cannot be archived and

    doesn’ t participate in state/preservation/restoration • Previously used to reduce memory footprint and prevent crashes • Solution: build our own by saving the last known URL
  14. WSOD - White Screen of Death • Seeing some webviews

    display about:blank for no reason upon navigating back • Appears to be a memory issue • Thought: is it fixed in iOS 9?
  15. WKWebView and self-signed certs • Bug in WKWebView where the

    documented way of doing custom analysis of SSL certificates is broken ◦ webView(_:didReceiveAuthenticationChallenge: completionHandler:) is *never* called • Important for internal use • No workaround
  16. Are others using it? • Firefox for iOS is built

    using WKWebView and is now in public preview (Sept. 3) • Some other apps have started using it
  17. WKWebView in Astro today • Currently disabled by default •

    Turned on in the Catalogue app • Hoping to try it on next release of ThinkGeek and hopefully Ritchie Bros. • Working on tests and seeing what is fixed in iOS 9 -- feeling solid for iOS 9 • Goal is to maintain both for now