Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView didn't finish loading, when didFinishNavigation is called - Bug in WKWebView?

Goal: To take a screenshot of WKWebView after the website finished loading

Method employed:

  • Defined a WKWebView var in UIViewController
  • Created an extension method called screen capture() that takes image of WKWebView

  • Made my UIViewController to implement WKNavigationDelegate

  • Set the wkwebview.navigationDelegate = self ( in the UIViewController init)

  • Implemented the didFinishNavigation delegation func in UIViewcontroller to call screen capture extension method for WKWebView

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {     let img = webView.screenCapture() } 

Questions:

  • When I debug i simulator, I notice that the control reaches the didFinishNavigation() func even though the website has not yet rendered in the WKWebView
  • Correspondingly the image screenshot taken is a white blob.

What am I missing here? I looked at all possible delegate functions for WKWebView and nothing else seem to represent the completion of content loading in WKWebView. Would appreciate help on if there is a work around


Update: Adding screenshot code that I am using to take a screenshot for web view

class func captureEntireUIWebViewImage(webView: WKWebView) -> UIImage? {      var webViewFrame = webView.scrollView.frame     if (webView.scrollView.contentSize != CGSize(width: 0,height: 0)){     webView.scrollView.frame = CGRectMake(webViewFrame.origin.x, webViewFrame.origin.y, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height)      UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize, webView.scrollView.opaque, 0)     webView.scrollView.layer.renderInContext(UIGraphicsGetCurrentContext())      var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()      UIGraphicsEndImageContext()       webView.scrollView.frame = webViewFrame               return image     }      return nil  } 
like image 286
shrutim Avatar asked May 17 '15 20:05

shrutim


People also ask

How do I refresh WKWebView?

The WKWebView already contains a scrollview. All you need to do is create the refresh control, assign a target function that will get called when a user initiates a refresh, and attach the refresh control to the scrollview.

How can I clear the contents of a UIWebView WKWebView?

To clear old contents of webview With UIWebView you would use UIWebViewDelegate 's - webViewDidFinishLoad: .

How do I migrate from UIWebView to WKWebView?

Step-1: Firstly, create a new project with a Single View App. Step-2: Then, open “Main. storyboard” and on your ViewController's view drag “Webkit View”. Select a WKWebView and place it on your view of a view controller.

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

For those still looking for an answer to this, the marked answer is BS, he just forced his way into getting it accepted.

Using property,

"loading" 

and

webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) 

both do the same thing, indicate if the main resource is loaded.

Now, that does not mean the entire webpage/website is loaded, because it really depends on the implementation of the website. If it needs to load scripts and resources (images, fonts etc) to make itself visible, you'll still see nothing after the navigation is completed, because the network calls made by the website are not tracked by the webview, only the navigation is tracked, so it wouldn't really know when the website loaded completely.

like image 134
Santhosh R Avatar answered Sep 16 '22 17:09

Santhosh R


WKWebView doesn't use delegation to let you know when content loading is complete (that's why you can't find any delegate method that suits your purpose). The way to know whether a WKWebView is still loading is to use KVO (key-value observing) to watch its loading property. In this way, you receive a notification when loading changes from true to false.

Here's a looping animated gif showing what happens when I test this. I load a web view and respond to its loading property through KVO to take a snapshot. The upper view is the web view; the lower (squashed) view is the snapshot. As you can see, the snapshot does capture the loaded content:

enter image description here

[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {     if (self->_webKitView.isLoading == true) {         NSLog(@"Still loading...");     }else {         NSLog(@"Finished loading...");         [timer invalidate];         dispatch_async(dispatch_get_main_queue(), ^{             [self->_activityIndicator stopAnimating];         });     } }]; 
like image 25
matt Avatar answered Sep 17 '22 17:09

matt