0

I'm new to PhoneGap/Cordova, and would like to call some javascript outside of the CDVPlugin interface.... In code that isn't part of a plugin.

Is this possible? If I could get a reference to the webview that PhoneGap uses, I should be able to call something like:

[webView stringByEvaluatingJavaScriptFromString:@"myJSFunction()"];

or

[super writeJavascript::@"myJSFunction()"];
Brad Parks
  • 66,836
  • 64
  • 257
  • 336

1 Answers1

0

You can do this for ios by getting a reference to the WebView from the AppDelegate:

+(void) fireJavascript:(NSString*) javascript
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    [appDelegate fireJavascript:javascript];
}

and in your AppDelegate, define a method like so:

- (void) fireJavascript:(NSString *)javascript
{
    NSLog(@"About to fire js:%@", javascript);
    [self.viewController.webView stringByEvaluatingJavaScriptFromString:javascript];
}

Note that you can get a reference for the app delegate in your app using info from this question on StackOverflow.

Community
  • 1
  • 1
Brad Parks
  • 66,836
  • 64
  • 257
  • 336