Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch JavaScript exceptions from WebView in ios

I need to catch errors that occur in WebView if WebView is present in the screen. That is not to bind to a specific WebView.

like image 358
Vadim Denisuk Avatar asked May 19 '16 10:05

Vadim Denisuk


People also ask

Does WebView work on iOS?

WebView can be defined as an object which can display the interactive web content and load HTML strings within the iOS application for an in-app browser. It is an instance of the WKWebView class, which inherits the UIView class.

How do I debug Safari WKWebView?

Debugging with WKWebViewOpen Safari and press ⌘+, to open Preference, under the Advanced tab, check “Show Develop menu in menu bar”. Not until we have the project build can we use the Safari debugger. The debugger is under Develop → Your simulator or device.

What is WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


1 Answers

Not sure if you have already tried this solution,You can catch the exception in your JS code and call a native method to log this information. You can inject this native method implementation into JS context of the UIWebview after a receiving below UIWebView delegate callback that JS context for the webView has been created.

- (void)webView:(id)webView didCreateJavaScriptContext:(JSContext *)context forFrame:(id)frame {
//this injects the MyNativeLog in your UIWebView JS context and you can call this function in your JS code.
context[@"MyNativeLog"] = ^(NSString *string){
            NSLog(@"%@", string);
        };
}

Hope this helps!!

like image 177
Chandra Avatar answered Sep 27 '22 20:09

Chandra