2

When loading a script using WebView.stringByEvaluatingJavaScriptFromString that contains syntax errors, the script is not loaded at all and no error is returned. I set up my WebView's context like this to report errors:

let context = view.mainFrame.javaScriptContext;

context.setObject(false, forKeyedSubscript: "JSError");
context.exceptionHandler = { context, exception in
  self.logIntern("JS Error: " + exception.toString());
  context.setObject(true, forKeyedSubscript: "JSError");
}

This reporting works nicely for runtime errors, but not parse errors. How can I know that parsing my script failed (maybe including parsing info, like error type and line number)?

1 Answer 1

1

You can achieve your goal using WebScriptDebugDelegate private API (first private header). You need to set your object as scriptDebugDelegate (second private header) for your WebView instance.

In script debug delegate you should implement following method: - (void)webView:(WebView *)webView failedToParseSource:(NSString *)source baseLineNumber:(unsigned)lineNumber fromURL:(NSURL *)url withError:(NSError *)error forWebFrame:(WebFrame *)webFrame. Other methods fro this protocol also look very promising.

I believe that answer in Objective-C is much better that nothing :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.