Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - Check if WKWebView has loaded page

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:

  • detect WKWebView finish loading

  • Call JavaScript function from native code in WKWebView

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!

like image 916
Stef Avatar asked Aug 01 '17 22:08

Stef


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!)

How can I clear the contents of a UIWebView WKWebView?

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

What is WKWebView in Swift?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


2 Answers

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!

like image 58
Stef Avatar answered Oct 14 '22 01:10

Stef


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") } 
like image 25
Sunil Targe Avatar answered Oct 14 '22 02:10

Sunil Targe