41

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!

Stef
  • 1,183
  • 1
  • 9
  • 12
  • 4
    In the first question you linked, it mentions the use of `WKNavigationDelegate`, and also `func webView(WKWebView, didFinish: WKNavigation!)`, did they help? – paulvs Aug 01 '17 at 23:10
  • Hi, thanks for your reply. It didn't help for me, but maybe I don't know how to implement this methods the right way. Do you know how to implement this method: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455629-webview ? – Stef Aug 02 '17 at 09:49
  • To use that method, make your view controller class conform to the `WKNavigationDelegate` protocol, then implement that method inside your view controller class. There should be examples online [such as this](https://iosdevcenters.blogspot.com/2016/05/creating-simple-browser-with-wkwebview.html). – paulvs Aug 02 '17 at 12:57
  • 1
    Thanks @paulvs ! Didn't know it was that easy to implement a method from apple documentation! – Stef Aug 02 '17 at 18:48
  • @Stef You should mark your question as answered :) – Adrian Jan 01 '20 at 16:37

2 Answers2

76

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!

Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
Stef
  • 1,183
  • 1
  • 9
  • 12
32

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")
}
Jay Gajjar
  • 2,661
  • 2
  • 21
  • 35
Sunil Targe
  • 7,251
  • 5
  • 49
  • 80