cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ally Ogilvie <>
Subject Re: CordovaPlugin intialized immediately
Date Thu, 10 Jul 2014 04:13:58 GMT
Hey thought I'd give some pro-tips to @Jan but also for anyone else who
might be listening.
I'll blog this post after cleaning it up a little, additions are be welcome.

There are several ways you can get an iOS plugin class to boot with, or
before the WebView is created. Here are my favs:

- (void)pluginInitialize (recommended method)

Set <param name="onload" value="true" /> in config.xml for this one.

- (CDVPlugin *)initWithWebView:(UIWebView *)theWebView;

You can hijack this method without onload, but you will been to execute a
JavaScript API first which means waiting for deviceready. My favourite way
to do this is adding a deviceready event listener into your plugin.js API
file's constructor.

Singleton classes...

Singletons are super badass. Create an extra class to be packaged with your
plugin class (this will be your main class with methods). Now your plugin
class will simply invoke the methods on the singleton.

Your main class will use the +(void)load to create a singleton for itself
which will be accessed by the plugin class. Check this example:
Creating a singleton
Accessing it in a plugin

Some words of warning +(void)load is so awesome it runs immediately when
the class is added to memory so careful when calling stuff outside your
main class, they probably won't exist yet xD

*and Android:*

public void initialize(CordovaInterface cordova, CordovaWebView webView)

This gets called as soon as the first JavaScript API gets called. Does NOT
currently adhere to <param name="onload" value="true" /> AFAIK, I usually
do something cheeky like with iOS' initWithWebView by adding a hidden fake
init method to the JavaScript API hooked onto the deviceready listener.

There might be another trick to pick up onMessage(String id, Object data)
and catch onPageFinished or something, not sure.

Android needs some love.

@Shazron can we get someone to verify at onload for Android?

On Thu, Jul 10, 2014 at 10:06 AM, Jan Veleck√Ĺ <> wrote:

> Yay!
> Nevertheless, in source code there are old initialize method, which is
> deprecated and new method, which cannot be overridden, so it is not
> executed, then I must use deprecated method. O_o

<>Ally Ogilvie
Lead Developer - MobDev. | Wizcorp Inc. <>
TECH . GAMING . OPEN-SOURCE WIZARDS+ 81 (0)3-4550-1448 | Website
<> | Twitter <> | Facebook
<> | LinkedIn

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