2
NSString* jsString = [NSString stringWithFormat:@"alert('ok');"];[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];

I wrote it in cordova appdelegate.m!

2
  • what version of cordova-ios platform are you building with? Commented Jan 20, 2016 at 11:35
  • cordova -v in terminal -> 5.4.1, but Xcode console display 'Apache Cordova native platform version 3.9.2 is starting.' when I start the app. Commented Jan 20, 2016 at 13:44

2 Answers 2

9

On [email protected] you should be able to do:

[self.webView stringByEvaluatingJavaScriptFromString:jsString];

On cordova-ios@4+ you will need to cast the Webview class as it also supports WKWebView:

if ([self.webView isKindOfClass:[UIWebView class]]) {
    [(UIWebView*)self.webView stringByEvaluatingJavaScriptFromString:jsString];
}
Sign up to request clarification or add additional context in comments.

2 Comments

<UIWebView: 0x14ddb1880; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x14dda85a0>>, but stringByEvaluatingJavaScriptFromString it does not work.
That's of course assuming your class extends CDVPlugin - I'm using the same approach in my own plugin and it works fine
0

May want to try NSString* jsString = [NSString stringWithString:@"alert('ok');"];[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString]; or NSString* jsString = @"alert('ok');"[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];

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.