WKWebView - the time has come to ditch old UIWebView

September 23, 2015

WKWebView - the time has come to ditch old UIWebView

We all have experienced these uncontrolled leaks from UIWebView, as well as not so great performance, compared to native Safari app. Performance drop can be explained - Safari app uses sweet Nitro JavaScript engine, that is unavailable in UIWebView. And the title of the article is not being made up by myself, no. I just repeat Apple's own words. Well, not exactly, but you got my drift.

A WKWebView object displays interactive web content. New development should employ this class instead of the older UIWebView class.
Source: WKWebView documentation

You've heard it. But WKWebView is here for a reason. It eliminates those leaks as well as improved performance with support of Nitro JavaScript engine plus you get a more fine-grained tuning API, for instance - ability to track page's load progress, finally! You can read about more enhancements and performance growth in this nice article: WKWebView vs UIWebView by Initlabs

So I am going to ride you through the typical uses of UIWebView, but from the WKWebView's perspective.

Instantiation and load of webpage

Creating and requesting URL to open is pretty straightforward, but with a couple more lines of code and configurations. Here is how you create WKWebView instance and ask it to open Google for you.

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://google.com"]]];
[self.view addSubview:webView];

Receiving webpage info

Now, this is something, that had to be done through UIWebView's delegate callbacks and with use of JavaScript. Now it is available is a simple property.

NSURL *URL = webView.URL;        // Get current/active URL
NSString *title = webView.title; // Get current page title

Tracking page loading progress

Now this is something we've all been lacking for so long and finally we have it. Such a neat property, yet now so simple to get - page load progress. Now you can display your own load progress bar with minimum effort. How minimum? This minimum:

[webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        if ([change[NSKeyValueChangeKindKey] integerValue] == NSKeyValueObservingOptionNew) {
            NSLog(@"%@", change[NSKeyValueChangeNewKey]);

The WebKit has a lot more to offer than I have covered here, along with delegated, navigation objects and stuff, all sorts of cool stuff. But I leave them to you to discover.

So this would probably be a typical use of WKWebView and I hope I have just made your transition from UIWebView a little bit easier.


comments powered by Disqus