3

I tried this but it didnt work. here is my phonegap index.html:

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
</body>
</html>

And here is my MainViewController.m method i want to trigger it:

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
adView.hidden = YES;

// jscript call
[webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
}

Is what i am trying to do even possible? I dont know objective-c at all so i am really lost here. Please help!

  • Javascript lives in a web browser client. Objective-C (or, for that matter, C++, C#, etc etc) run at the platform level. Two completely separate worlds. – paulsm4 Feb 13 '13 at 03:47
  • I understand that but i thought it would be possible by now. look at this http://stackoverflow.com/questions/7719881/can-you-call-a-javascript-function-from-native-code-not-in-a-callback-using-ph i didnt understand the answer so i couldnt do this. – user2052886 Feb 13 '13 at 03:52

2 Answers2

5

Yes, you can absolutely do that!

The "stringByEvaluatingJavaScriptFromString" does the trick.

Here is the syntax you need apply in the place where you need to trigger :

[self.webView stringByEvaluatingJavaScriptFromString:@"YOUR JAVASCRIPT FUNCTION()"];
GenieWanted
  • 4,473
  • 4
  • 24
  • 35
-2

The whole idea in using phonegap from my understanding is so that you don't need to learn objective-c. You use HTML5, CSS and Javascript libraries like JQuery to make the app do what you want.

You can use the phonegap.js API and plugins to access native features.

OrganizedChaos
  • 431
  • 2
  • 11
  • Yes, but in my case i need to. What i posted isnt what im using it for it was just a simple way to explain what i need to be able to do. – user2052886 Feb 13 '13 at 03:49
  • Got ya... I've been using Phonegap for a couple of months and didn't read this to be possible, but I am sure someone here will have an answer if there is one. Good luck. – OrganizedChaos Feb 13 '13 at 04:18