How to switch to WKWebView?

I am trying to understand how to use the new WKWebView in iOS8, can not find much information. I 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 applications? Will regular UiWebView get acceleration from nitro java script or do we need to make changes? How do we deal with backward compatibility?

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

Thanks for any help on this!

+50
ios8 uiwebview wkwebview
Sep 04 '14 at 12:09 on
source share
9 answers

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

To use this faster JavaScript engine in older applications, you need to make code changes to use WKWebView instead of UIWebView . For iOS7 and older, you should continue to use UIWebView , so you may need to check iOS8 and then apply the WKWebView delegate methods / methods and return to the UIWebView methods for iOS7 and older. There is also no Interface Builder component for WKWebView (yet), so you need to programmatically implement WKWebView .

You can implement WKWebView in Objective-C, here is a simple example to run 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 use webview to load a simple html or website, you can continue to use UIWebView .

Here is an app test that can be used to open any website using UIWebView or WKWebView , and you can compare the performance, and then decide to upgrade the application to use WKWebView : https://itunes.apple.com/app/id928647773?mt = 8 & at = 10ltWQ

enter image description here

+50
Oct 25 '14 at 4:34
source share

This is how I switched from UIWebView to WKWebView .

Note. There is no such property as UIWebView that can be dragged onto storyboards; you must do this programmatically.

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 
+30
Dec 04 '14 at 19:27
source share

Step: 1 Import webkit into ViewController.swift

 import WebKit 

Step: 2 Declare the webView variable.

 var webView : WKWebView! 

Step: 3 Adding a WKNavigationDelegate Delegate

 class ViewController: UIViewController , WKNavigationDelegate{ 

Step: 4 Adding code to ViewDidLoad .

 let myBlog = "https://iosdevcenters.blogspot.com/" let url = NSURL(string: myBlog) let request = NSURLRequest(URL: url!) // init and load request in webview. webView = WKWebView(frame: self.view.frame) webView.navigationDelegate = self webView.loadRequest(request) self.view.addSubview(webView) self.view.sendSubviewToBack(webView) 

Step: 5 Edit adding info.plist

 <dict> <key>NSAllowsArbitraryLoads</key> <true/> <key>NSExceptionDomains</key> <dict> <key>google.com</key> <dict> <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSIncludesSubdomains</key> <true/> </dict> </dict> 
+14
Jun 01 '16 at 6:19 06:19
source share

Swift is not a requirement; everything works fine with Objective-C. UIWebView will continue to be supported, so there is no need to rush the port if you want to spend your time. However, it will not receive Javascript improvements and WKWebView scrolling.

For backward compatibility, I have two properties for my controller: UIWebView and WKWebView. I use WKWebview only if the class exists:

 if ([WKWebView class]) { // do new webview stuff } else { // do old webview stuff } 

While I had a UIWebViewDelegate, I also made it WKNavigationDelegate and created the necessary methods.

+9
Sep 16 '14 at 16:37
source share

WkWebView is much faster and more reliable than UIWebview according to Apple docs . Here I posted my WkWebViewController.

 import UIKit import WebKit class WebPageViewController: UIViewController,UINavigationControllerDelegate,UINavigationBarDelegate,WKNavigationDelegate{ var webView: WKWebView? var webUrl="http://www.nike.com" override func viewWillAppear(animated: Bool){ super.viewWillAppear(true) navigationController!.navigationBar.hidden = false } override func viewDidLoad() { /* Create our preferences on how the web page should be loaded */ let preferences = WKPreferences() preferences.javaScriptEnabled = false /* Create a configuration for our preferences */ let configuration = WKWebViewConfiguration() configuration.preferences = preferences /* Now instantiate the web view */ webView = WKWebView(frame: view.bounds, configuration: configuration) if let theWebView = webView{ /* Load a web page into our web view */ let url = NSURL(string: self.webUrl) let urlRequest = NSURLRequest(URL: url!) theWebView.loadRequest(urlRequest) theWebView.navigationDelegate = self view.addSubview(theWebView) } } /* Start the network activity indicator when the web view is loading */ func webView(webView: WKWebView,didStartProvisionalNavigation navigation: WKNavigation){ UIApplication.sharedApplication().networkActivityIndicatorVisible = true } /* Stop the network activity indicator when the loading finishes */ func webView(webView: WKWebView,didFinishNavigation navigation: WKNavigation){ UIApplication.sharedApplication().networkActivityIndicatorVisible = false } func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse,decisionHandler: ((WKNavigationResponsePolicy) -> Void)){ //print(navigationResponse.response.MIMEType) decisionHandler(.Allow) } override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } } 
+7
Oct 30 '15 at 9:15
source share

WKWebView using Swift in iOS 8 ..

The entire ViewController.swift file now looks like this:

 import UIKit import WebKit class ViewController: UIViewController { @IBOutlet var containerView : UIView! = nil var webView: WKWebView? override func loadView() { super.loadView() self.webView = WKWebView() self.view = self.webView! } override func viewDidLoad() { super.viewDidLoad() var url = NSURL(string:"http://www.kinderas.com/") var req = NSURLRequest(URL:url) self.webView!.loadRequest(req) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 
+5
Sep 24 '14 at 16:41
source share

Use some design patterns, you can mix UIWebView and WKWebView. The key is to create a unique browser interface. But you should pay more attention to the current functionality of your application, for example: if your application uses NSURLProtocol to increase the network's ability using WKWebView, you have no chance to do the same. Since NSURLProtocol only affects the current process, and WKWebView uses a multi-level process architecture, the network staff is in a separate process.

+2
Oct. 10 '14 at 4:00
source share

You need to use WKWebView, which is available in iOS8 in the 'WebKit' Framework to get acceleration. If you need backward compatibility, you should use UIWebView for iOS7 and later.

I set up a little code to provide a UIViewController frame for the new WKWebView. It can be installed through cocoapods. Look at here:

STKWebKitViewController on github

+1
Sep 07 '14 at 16:27
source share

Swift 4

  let webView = WKWebView() // Set Frame as per requirment, I am leaving it for you let url = URL(string: "http://www.google.com")! webView.load(URLRequest(url: url)) view.addSubview(webView) 
0
Nov 29 '17 at 9:36 on
source share



All Articles