There is no way to do this using evaluateJavaScript, since it's callback is called as soon as the evaluated Javascript returns. This is most likely before your asynchronous call is done.
You will need to register a message handler in your swift code similar to…
let script = WKUserScript(source: javascriptString,
injectionTime: injectionTime,
forMainFrameOnly: true)
userContentController.addUserScript(script)
webView.configuration.userContentController.addScriptMessageHandler(self,
name: "didShowLoading")
You can then define a delegate method in your Swift code…
func userContentController(userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage) {
if message.name == "didShowLoading" {
// do something
}
}
Finally, you can post the message from your javascript code…
var script = "$('.loading-gif').removeClass('hide', function() {"
+ " webkit.messageHandlers['didShowLoading'].postMessage('');"
+ "});"
webView.evaluateJavaScript(script)