You can use EasyJSWebView to easily implement calling Native Objective-C method from JavaScript.
The more convenient way is to implement a class that holds all your methods that your JS will call and instantiate that class with your UIWebView Control Object subclass with EasyJSWebView as an argument.
For example:
I created a class JSInterface to hold all my native Obj-C methods
I have a custom method
- (void)initializeJSInterfaceForWebView:(EasyJSWebView *)webView {
self.jsInterface.appDelegate = appDelegate;
self.jsInterface.webView.delegate = self;
[webView addJavascriptInterfaces:self.jsInterface WithName:@"nativeInterface"];
}
that will initialized EasyJSWebView.
I instantiate
self.jsInterface = [JSInterface initWithView:self];
[self initializeJSInterfaceForWebView:self.webView];
For instance, we have a native Objective-C method:
- (void) printLog:(NSString *)msg {
NSLog(@"%@", msg);
}
To call on the native method from JavaScript using the nativeInterface object name as follows:
nativeInterface.printLog("This is only a test.");