WKWebView evaluateJavascript method crash with async/await when javascript call doesn't have return value

I wrote a code like the example below to execute javascript code that has no return value.

let webView: WKWebView

// after load complete
let result = await webView.evaluateJavascript("someFunction()")  // :0: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

but when i use method with completion handler manner, it doesn't be crashed. but Xcode enforces me to use 'await' keyword and warning is bordering me

await webView.evaluateJavaScript("someFunction()", completionHandler: nil) // warning: Consider using asynchronous alternative function

The differnce I found is the different signature. Completion handler version has Optional result type, but async/await version has just Any result type

func evaluateJavaScript(_ javaScriptString: String, 
      completionHandler: ((Any?, Error?) -> Void)? = nil)

func evaluateJavaScript(_ javaScriptString: String) async throws -> Any

my Xcode version is 13.2.1. Hope to fix it soon.

You can define your async func like this

extension WKWebView {    

    @discardableResult
    func evaluateJavaScriptAsync(_ str: String) async throws -> Any? {
        return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Any?, Error>) in
            DispatchQueue.main.async {
                self.evaluateJavaScript(str) { data, error in
                    if let error = error {
                        continuation.resume(throwing: error)
                    } else {
                        continuation.resume(returning: data)
                    }
                }
            }
        }
    }
}

Man this sucks!! Like async await is supposed to make life easier or worse?

Apple please fix this!

Still an issue with Xcode 15 & Swift 5.9!

Non-async version gives a useless warning and async version crashes the app with :0: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value in case where function doesn't return anything. Any hope for a fix?

Also having the same issue here, it almost seems like the callback isn't being passed in properly and is nil

Same issue Xcode 15.2 & Swift 5.9.2

WKWebView evaluateJavascript method crash with async/await when javascript call doesn't have return value
 
 
Q