You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by Mirko Luchi <mi...@gmail.com> on 2013/05/21 13:45:34 UTC

Embedding CDVViewController in native app - When is it safe to call JavaScript code from iOS?

Hi.

I have an HTML+JS widget that I'd like to use in a native iOS app.
My idea was to create a subclass of CDVViewController that loads the
HTML+JS code of my widget, and expose some native APIs that actually call
JavaScript functions.

The CDVViewController let me access the underlying UIWebView through the
webView property, so that I can call -[UIWebView
stringByEvaluatingJavaScriptFromString:] to execute some arbitrary JS code.

So I would do something like this:

@interface MyWidget : CDVViewController

-(NSString*)myAPIFunction;

@end

@implementation MyWidget

-(NSString*) myAPIFunction{
return [self.webView    stringByEvaluatingJavaScriptFromString:@
"someJavaScriptFunction();"];
}

@end

Problem is that I must make sure to call JavaScript code ONLY AFTER I'm
sure the UIWebView has finished loading the HTML code, otherwise the
JavaScript functions I need to call woud not be available yet.
What is the correct way to achieve this?
Should I register for the CDVPageDidLoadNotification and call JavaScript
from iOS only after I receive that notification or is there another way to
do that?

Thanks.

Mirko

Re: Embedding CDVViewController in native app - When is it safe to call JavaScript code from iOS?

Posted by Andrew Grieve <ag...@chromium.org>.
The PageDidLoad event fires at the same time that window.onload fires, so
that's probably the easiest thing to use. The other option would be to
create a custom plugin and call it from JS after onDeviceReady().


On Tue, May 21, 2013 at 7:45 AM, Mirko Luchi <mi...@gmail.com> wrote:

> Hi.
>
> I have an HTML+JS widget that I'd like to use in a native iOS app.
> My idea was to create a subclass of CDVViewController that loads the
> HTML+JS code of my widget, and expose some native APIs that actually call
> JavaScript functions.
>
> The CDVViewController let me access the underlying UIWebView through the
> webView property, so that I can call -[UIWebView
> stringByEvaluatingJavaScriptFromString:] to execute some arbitrary JS code.
>
> So I would do something like this:
>
> @interface MyWidget : CDVViewController
>
> -(NSString*)myAPIFunction;
>
> @end
>
> @implementation MyWidget
>
> -(NSString*) myAPIFunction{
> return [self.webView    stringByEvaluatingJavaScriptFromString:@
> "someJavaScriptFunction();"];
> }
>
> @end
>
> Problem is that I must make sure to call JavaScript code ONLY AFTER I'm
> sure the UIWebView has finished loading the HTML code, otherwise the
> JavaScript functions I need to call woud not be available yet.
> What is the correct way to achieve this?
> Should I register for the CDVPageDidLoadNotification and call JavaScript
> from iOS only after I receive that notification or is there another way to
> do that?
>
> Thanks.
>
> Mirko
>