Swift 3 - Check if WKWebView has loaded page

43,328

Solution 1

Answer (Big thanks to @paulvs )

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!) {
    print("loaded")
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL(string: "https://www.yourwebsite.com/") !
    let request = URLRequest(url: url)
    webView.navigationDelegate = self
    webView.load(request)


    // Do any additional setup after loading the view, typically from a nib.
  }

}
  1. Add WKNavigationDelegate to class
  2. Add:

    func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) { print("loaded") }
    

Result: It will print "loaded" in the console everytime the WKWebView has finished loading the page. This was excactly what I was looking for, so again a big thanks to Paulvs!

Solution 2

Set delegate > WKNavigationDelegate

Objective-C

// Start loading WKWebView
-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    NSLog(@"Start loading");
} 

//Finished loading WKWebView
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"End loading");
}

Swift 4.2

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
    print("Start loading")    
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    print("End loading")
}
Share:
43,328
Stef
Author by

Stef

Updated on June 28, 2020

Comments

  • Stef
    Stef almost 4 years

    My question: How do I check if a page in WKWebView has fully loaded in Xcode using Swift 3?

    This is my problem:

    Webpage 1: From this page I load Webpage 2

    Webpage 2: I need to get html data from Webpage 2, but when I print the HTML data I get HTML data of webpage 1, which I don't want. But when I print HTML data 2 seconds later it gives me the right HTML data.

    I need to know whether or not a page in WKWebView did finish loading. I can see in the WebView it is loaded and also the progressbar is fully loaded, but when I print html data of the page I get html data of previous page, which is not what I want. Only if I wait a second it gives the right data, probably cause Webpage 2 is loaded.

    So how do I let Xcode to print html when the next page is totally loaded?

    I have tried several methods:

    Maybe I can use:

    if webView.isloading { get } but I don't know how to implement this method and if it should work. I have tried several methods from Stack but these are not working for me or outdated.

    Do you guys know a solution for this problem in Swift 3? Thanks!

  • Nikita
    Nikita over 5 years
    this is not correct, it is per each resource, but not for the whole page with all the resources;
  • Nikita
    Nikita over 5 years
    also there might be redirects which also needs additional treatment
  • Jason
    Jason over 4 years
    Missed adding this line: webView.navigationDelegate = self; -- works perfectly after adding!