cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mirko Luchi <mirkolu...@gmail.com>
Subject Embedding CDVViewController in native app - When is it safe to call JavaScript code from iOS?
Date Tue, 21 May 2013 11:45:34 GMT
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