Swift 3 - Check if WKWebView has loaded page
Answer (Big thanks to @paulvs )
To check if your WKWebView has loaded easily implement the following method:
import WebKitimport UIKitclass 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. }}
- Add WKNavigationDelegate to class
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!
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")}