Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when WKWebView has finished loading EVERY time

I have a WKWebView app running on iOS8 on iPad (standard iPad UserAgent : "Mozilla/5.0 (iPad; CPU OS 8_1_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B436 Safari/600.1.4"). I have tried all delegates and listeners I can think of to try and detect when a page has finished loading, every time. Here is the problem:

  1. Open your WKWebView and go to Google. The following are called:

    _decidePolicyForNavigationAction
    _didStartProvisionalNavigation
    _ didFinishNavigation
    
  2. Type “YouTube” into Google: ONLY _decidePolicyForNavigationAction is called. No didStartProvisionalNavigation or didFinishNavigation

  3. Click on YouTube within Google. The following are called:

    _didStartProvisionalNavigation
    _decidePolicyForNavigationAction
    _ didFinishNavigation
    
  4. From now on, within YouTube nothing is called. Click on a YouTube video and no didStartProvisionalNavigation or didFinishNavigation. Also, webview.loading observer is no longer called. Even decidePolicyForNavigationAction is only called every now and then. HOWEVER… The webView.backForwardList.currentItem is updated after every click, so this must be detecting when the page has finished loading somehow?

This behavior happens on a lot of other sites too (Vimeo for example). I know this type of site is not updating the main frame every time, but is this lack of a capability to detect when loading has started/finished a ‘limitation’ of WKWebView in its current state?

What I want to achieve is: Is there another way to detect when navigation has COMPLETED, EVERY TIME within Java mobile websites like YouTube/Vimeo:

  • Using NSURLProtocol or similar?
  • Or maybe a way to detect when webView.backForwardList.currentItem is updated?

Thanks for any help.

like image 500
AT3D Avatar asked Apr 13 '15 10:04

AT3D


People also ask

How do I know if WKWebView finished loading Swift?

To check if your WKWebView has loaded easily implement the following method: import WebKit import UIKit class ViewController: UIViewController, WKNavigationDelegate { let webView = WKWebView() func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)

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.

What is the difference between UIWebView and WKWebView?

Difference Between UIWebview and WKWebView UIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.

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.


1 Answers

I'm not sure what exactly you want to do when page have loaded, but hopefully observing these properties can help you:

webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)
webView.addObserver(self, forKeyPath: "loading", options: .New, context: nil)
webView.addObserver(self, forKeyPath: "title", options: .New, context: nil)
webView.addObserver(self, forKeyPath: "canGoBack", options: .New, context: nil)
webView.addObserver(self, forKeyPath: "canGoForward", options: .New, context: nil)
like image 68
zh. Avatar answered Oct 23 '22 08:10

zh.