UIWebView vs. WkWebView

UIWebView vs. WkWebView

2018, Jun 11    

Comparison between WKWebView and UIWeview, pros and cons.

Advantages of WkWebView

Since iOS version 8.0, apple officially launched WkWebView, as compared to the previous UIWebView, it has the following advantages:

  • Most of the existing delegate methods like load request, provide navigation forward backward are compatible
  • Based on the compatibility for UIWebView, wkwebview does provide further enhancement on webpage navigation control. Now it supports list of forward and back items and users can jump to specific items on the browsing history instead of previously forward and backward. It also supports the loading indicator progress bar, with it , there is no need further develop your own progress bar
  • What is more, the loading speed and memory allocation efficiency in wkwebview has been dramatically increased. refer to this

Disadvantages of WkWebView

WKWebView does not very welly support URLProtocol, to configure and use URLProtocol, need to hack into the system’s private headers: (contextController)

  // Inject UR own protocol
  [NSURLProtocol registerClass:[CustomProtocol class]];

  //Initialization of WKWebView
  WKWebViewConfiguration * config = [[WKWebViewConfiguration alloc] init];
  WKWebView * wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) configuration:config];
  [wkWebView loadRequest:webViewReq];
  [self.view addSubview:wkWebView];

  Class cls = NSClassFromString(@"WKBrowsingContextController");
  SEL sel = NSSelectorFromString(@"registerSchemeForCustomProtocol:");
  if ([cls respondsToSelector:@selector]) {
    // register http and https protocols
    [cls performSelector:sel withObject:@"http"];
    [cls performSelector:sel withObject:@"https"];
  }
TOC