Search code examples
iosswiftxcodewkwebview

WKWebView didCommit not calling when uploading file


I'm using the newest version of Xcode and Swift.

In my app there's a WKWebView that loads a page that contains an input field to upload files to my server.

I have the following code in my app:

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        networkActivityIndicator.startAnimating()
    }

This will show a network activity indicator while a webpage is loading.

For some reason, this isn't called when I start uploading the file. This will get called when uploading the file is ready:

  1. I select a file and tap to submit/upload the file.
  2. The file upload starts (this may take a while), WKWebView didCommit doesn't get triggered.
  3. File upload is ready and the webpage reloads. Now WKWebView didCommit gets called.

Why doesn't WKWebView didCommit get called as soon as I submit my form to upload the file?


Solution

  • The solution was to add startAnimating() to WKWebView decidePolicyFor like this:

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
            decisionHandler(.allow)
            networkActivityIndicator.startAnimating()
    }