What Is Wkerrordomain Error 4 from Wkwebview

What is WKErrorDomain error 4 from WKWebView

So if we dig into the headers:

/*! @constant WKErrorDomain Indicates a WebKit error. */
@availability(iOS, introduced=8.0)
let WKErrorDomain: String

/*! @enum WKErrorCode
@abstract Constants used by NSError to indicate errors in the WebKit domain.
@constant WKErrorUnkcnown Indicates that an unknown error occurred.
@constant WKErrorWebContentProcessTerminated Indicates that the Web Content process was terminated.
@constant WKErrorWebViewInvalidated Indicates that the WKWebView was invalidated.
@constant WKErrorJavaScriptExceptionOccurred Indicates that a JavaScript exception occurred.
*/
@availability(iOS, introduced=8.0)
enum WKErrorCode : Int {

case Unknown
case WebContentProcessTerminated
case WebViewInvalidated
case JavaScriptExceptionOccurred
}

Error code 4 would corresponds to JavaScriptExceptionOccurred, or WKErrorJavaScriptExceptionOccurred.

In other words, the JavaScript function causes some error.

Probably not more here that you couldn't guess already. For resolution, I would suggest using the developer features of a web browser such as Safari, loading the HTML and debugging.

In fact, as explained in the WWDC 2014 video, "Introducing the Modern WebKit API", your desktop Safari browser can "inspect the WKWebView using the Safari web inspector, including any user scripts that you've injected."

To use this, while the WKWebView is loaded in memory in your running app on the iOS simulator, open the desktop Safari browser and access Develop on the top menu bar then iOS Simulator. That will show a drop down of the web view's document objects.

For more info on debugging JavaScript, take a look at Web Inspector: Understanding Stack Traces

WKErrorDomain error 4 from WKWebView when calling existing JSFunction

You can use the Safari Web Inspector to get more information about what is going wrong.

You can enable it by making sure the developer menu is enabled.

Show Developer Menu in Menu Bar

After the simulator starts your app, you can go to Developer->Simulator->[Your Page]. Once the Web Inspector is open you can refresh your web view and you should see a "console error". This should give you more insight into your "JavaScript exception".

WKWebview evaluateJavascript is not working, throws an error

Found a solution, it was simple as I was expecting, I was adding the javascript before the view complete load the content (before the Dom Ready). So I just had to move my code to the delegate method below:

  • webView:didFinishNavigation: (from WKNavigationDelegate)

I hope this helps someone.

A JavaScript exception occurred while highlighting text in WKWebview

Issue is, Userscript wasn't added properly.

let userContentController = WKUserContentController()
userContentController.add(messageHandler, name: "jsBridge")
let audioString = Bundle.main.url(forResource: "GetAudioLinks", withExtension: "js", subdirectory:"/Assets/js/" )

let audioStringScript = WKUserScript(source: audioString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
userContentController.addUserScript(audioStringScript)

now, it's working properly.



Related Topics



Leave a reply



Submit