WKWebView - the time has come to ditch old UIWebView
September 23, 2015

We all have experienced these uncontrolled leaks from
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
So I am going to ride you through the typical uses of
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
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
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