cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Grieve <agri...@chromium.org>
Subject Re: Embedding CDVViewController in native app - When is it safe to call JavaScript code from iOS?
Date Tue, 21 May 2013 14:30:11 GMT
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 <mirkoluchi@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
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message