How to call javascript function in i8 phonegap app from objective-c method?

I tried this but it didn't 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 call 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 don't know objective-c at all, so I am really lost here. Please, help!

+3


source to share


2 answers


Yes, you can absolutely do it!

" stringByEvaluatingJavaScriptFromString

" does the trick.



Here is the syntax you need to apply wherever you need to call:

[self.webView stringByEvaluatingJavaScriptFromString:@"YOUR JAVASCRIPT FUNCTION()"];

      

+5


source


The whole idea of ​​using telephony from my understanding is that you don't need to learn objective-c. You use HTML5, CSS and Javascript libraries like JQuery to make the application do what you want it to do.



You can use the phonegap.js API and plugins to access your own functionality.

-2


source







All Articles