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.
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
Capture Browser Console Logs with Capybara
JavaScript Module Pattern with Example
Escape String - Output Rails String in JavaScript
Template Language That Works on Both Server and Client
Why Is It Frowned Upon to Modify JavaScript Object's Prototypes
Detect If Hovering Over Element with Jquery
What's the Difference Between Returning Value or Promise.Resolve from Then()
Rails 5 - Turbolinks 5,Some Js Not Loaded on Page Render
R Shiny Build Links Between Tabs
Can Nokogiri Interpret JavaScript? - Web Scraping
How to Print an Iframe from JavaScript in Safari/Chrome
How to Use JavaScript in Ruby on Rails