WKWebView crashes while trying to evaluate JavaScript on Xcode 14.1, using Swift, tested on iOS but same behaviour should be on macOS.
I made a vastly simplified example to try to find a solution, and it keeps crashing:
let webView = WKWebView()
Task {
try? await webView.evaluateJavaScript("console.log('hello world')")
}
:0: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Seems that part of the problem is method overload, as of Xcode 14.1 there are several methods named
evaluateJavaScriptas part ofWKWebView.Due to optional parameters they seem to have the same signature, and the compiler is having a hard time understanding which one we mean.
Methods
After testing different scenarios it seems that when using
async/awaitversion of these methodsWKWebViewexpects JavaScript to return with a value (something other thanVoid), if there is no value returning from the JavaScript that you evaluate you will have a crash.Solution
Option 1
Always make sure JavaScript returns a value.
Crashing:
Not crashing:
Option 2
When not possible to return a value explicitly use the signature with a completion handler (even if you pass nil as the handler).