I'm working with cordova version 6.5.0 and I'm trying to develop a plugin that can execute some javascript code.
I've read on various forums that I can use stringByEvaluatingJavascriptFromString method in my webview, but it seems like it's not recognized as a valid method.
As a test, I tried the following code snippet in my AppDelegate.m file:
[self.viewController.webView stringByEvaluatingJavascriptFromString:@"alert('Test')"];
However, I'm getting an error saying "No visible @interface for 'UIView' declares the selector 'stringByEvaluatingJavascriptFromString'".
In order to solve this issue, I created a new class named Utils.m:
@implementation Utils: NSObject
id webview;
id delegate;
-(void)initialise:(id)wbview Delegate:(id)dlg {
webview = wbview;
delegate = dlg;
}
-(void)executeJavascript:(NSString *)str {
[delegate runInBackground:^{
[delegate stringByEvaluatingJavaScriptFromString:@"alert('test')"];
}];
}
Then, within my cordova plugin's pluginInitialize method, I have:
- (void)pluginInitialize {
utils = [[Utils alloc] init];
[utils initialise:self.webView Delegate:self.commandDelegate];
[utils executeJavascript:@"alert('Test');"];
}
Even though the stringByEvaluatingJavascriptFromString method seems to be accepted now, the application still crashes...
Any advice or suggestions would be greatly appreciated.
Thanks, Symeon