cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ally Ogilvie <aogil...@wizcorp.jp>
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
https://github.com/aogilvie/phonegap-plugin-wizAnalytics/blob/v3.3/platforms/ios/HelloCordova/Plugins/WizAnalyticsPlugin/WizAnalytics.m#L31
Accessing it in a plugin
https://github.com/aogilvie/phonegap-plugin-wizAnalytics/blob/v3.3/platforms/ios/HelloCordova/Plugins/WizAnalyticsPlugin/WizAnalyticsPlugin.m#L20

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:*

@Override
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√Ĺ <VVelda@seznam.cz> 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




-- 
<http://www.wizcorp.jp/>Ally Ogilvie
Lead Developer - MobDev. | Wizcorp Inc. <http://www.wizcorp.jp/>
------------------------------
TECH . GAMING . OPEN-SOURCE WIZARDS+ 81 (0)3-4550-1448 | Website
<http://www.wizcorp.jp/> | Twitter <https://twitter.com/Wizcorp> | Facebook
<http://www.facebook.com/Wizcorp> | LinkedIn
<http://www.linkedin.com/company/wizcorp>

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