Detect When Wkwebview Is Finished Loading

How to check if WkWebView finish loading in Objective-C?

I think the WKNavigationDelegate's webView:didFinishNavigation: delegate callback is what you're looking for.

Configure and present your activity indicator when you start to load and then stop and remove it from view when the callback is called.

How to check if wkwebview finished loading in UItest

I had some problems trying to figure out how to make it too, so I decide to use this work around. In the wkwebview didfinishload if you have any variable there, you can check if it exists or its value. If you do not have any variable there, you must create it in global scope, right? For example if we have this code when wkwebview ends loading:

func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) {
myTopLabel.text = "Content Loaded"
print("loaded")
}

'myTopLabel' is a UILabel that I use to show the status of my web, so what I had to do is execute this command in UITest:

let nextGameLabel = self.app.staticTexts["Content Loaded"]
app.buttons["Reload Web"].tap()
XCTAssert(nextGameLabel.waitForExistence(timeout: 5))

So When I press the app button, the UITest waits for 5 seconds to see if the web is loaded or not. Hope it helps dude.

Hiding view when WKWebView has finished loading swiftUI

Your View won't know to reload unless it is triggered with something like a @State property or an ObservableObject. You haven't really provided enough code from your SwiftUiWebView to show everything, but here's the gist of what needs to happen:

struct ContentView: View {
@State var webViewFinishedLoading = false

var body: some View {
SwiftUiWebView(url: URL(string: "myUrl"), finishedLoading: $webViewFinishedLoading)
ZStack {
if (!webViewFinishedLoading) {
....
}
}
}
}
struct SwiftUiWebView : UIViewRepresentable {
var url: URL
var finishedLoading: Binding<Bool>

//...
}

Then, you will need to pass that finishedLoading Binding to your web view delegate and set its .wrappedValue to true when you're done loading:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if (webView.isLoading) {
return
}
print("Done Loading")
finishedLoading.wrappedValue = true
}

Because it's tied to a @State variable, your view will know to refresh.

Detect when the WebView finishes loading in Swift4?

Three fatal issues:

  • You have to set the delegate

    WebView1.delegate = self
  • The signature of the delegate method is wrong

    func webViewDidFinishLoad(_ webView: UIWebView)
  • The delegate method must be on the top level of the class.

I think there is a fourth fatal issue: I doubt that WKWebView conforms to UIWebViewDelegate at all. I suppose you have to adopt WKNavigationDelegate and implement webView(:didFinish:) instead.

And please conform to the Swift naming convention that variable and function / method names start with a lowercase letter.

How to listen for when WkWebkiew has finished loading page?

How is _formHist defined??

If this isn't defined as a javascript object already, it won't work.

Apart from that, the page may not be fully loaded at the time the code is executed so you may need to listen to an event that is triggered when the page is fully loaded

If you refer to this answer it tells you that you can determine when the page has fully loaded using the following method

document.addEventListener("DOMContentLoaded", function(event) { 
//do work
});

So for your case, you just need to use this event listener

webView.evaluateJavaScript("document.addEventListener("DOMContentLoaded", function(event) { _formHist.submit(); });", completionHandler: nil)

Give that a try and see if it works



Related Topics



Leave a reply



Submit