Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Migrate to WKWebView?

I'm trying to understand how to make use of the new WKWebView in iOS8, can't find much information. I've read:

http://developer.telerik.com/featured/why-ios-8s-wkwebview-is-a-big-deal-for-hybrid-development/ http://nshipster.com/wkwebkit/

But how does this affect existing apps? Will an ordinary UiWebView get the speedup from the nitro java script engine or do we need to make changes? How do we deal with backwards compatibility?

All the code and examples I can find are using swift, will this be mandatory?

Thankful for any help on this matter!

like image 536
Daniel Åkesson Avatar asked Sep 04 '14 12:09

Daniel Åkesson


People also ask

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

Is WKWebView deprecated?

Since then, we've recommended that you adopt WKWebView instead of UIWebView and WebView — both of which were formally deprecated. New apps containing these frameworks are no longer accepted by the App Store.

How do I import WKWebView into Objective C?

You can implement WKWebView in Objective-C, here is simple example to initiate a WKWebView : WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self. view. frame configuration:theConfiguration]; webView.


2 Answers

UIWebView will still continue to work with existing apps. WKWebView is available starting from iOS8, only WKWebView has a Nitro JavaScript engine.

To take advantage of this faster JavaScript engine in older apps you have to make code changes to use WKWebView instead of UIWebView. For iOS7 and older, you have to continue to use UIWebView, so you may have to check for iOS8 and then apply WKWebView methods / delegate methods and fallback to UIWebView methods for iOS7 and older. Also there is no Interface Builder component for WKWebView (yet), so you have to programmatically implement WKWebView.

You can implement WKWebView in Objective-C, here is simple example to initiate a WKWebView:

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration]; webView.navigationDelegate = self; NSURL *nsurl=[NSURL URLWithString:@"http://www.apple.com"]; NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl]; [webView loadRequest:nsrequest]; [self.view addSubview:webView]; 

WKWebView rendering performance is noticeable in WebGL games and something that runs complex JavaScript algorithms, if you are using webview to load a simple html or website, you can continue to use UIWebView.

Here is a test app that can used to open any website using either UIWebView or WKWebView and you can compare performance, and then decide on upgrading your app to use WKWebView: https://itunes.apple.com/app/id928647773?mt=8&at=10ltWQ

enter image description here

like image 99
krisrak Avatar answered Sep 18 '22 02:09

krisrak


Here is how I transitioned from UIWebView to WKWebView.

Note: There is no property like UIWebView that you can drag onto your storyboard, you have to do it programatically.

Make sure you import WebKit/WebKit.h into your header file.

This is my header file:

#import <WebKit/WebKit.h>  @interface ViewController : UIViewController  @property(strong,nonatomic) WKWebView *webView; @property (strong, nonatomic) NSString *productURL;  @end 

Here is my implementation file:

#import "ViewController.h"  @interface ViewController ()  @end   @implementation ViewController  - (void)viewDidLoad {     [super viewDidLoad];     self.productURL = @"http://www.URL YOU WANT TO VIEW GOES HERE";      NSURL *url = [NSURL URLWithString:self.productURL];     NSURLRequest *request = [NSURLRequest requestWithURL:url];      _webView = [[WKWebView alloc] initWithFrame:self.view.frame];       [_webView loadRequest:request];     _webView.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);     [self.view addSubview:_webView]; }  - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. }  @end 
like image 40
Ali Avatar answered Sep 19 '22 02:09

Ali