Skip to Navigation | Skip to Content



iPhoneGap Plugs | April 1st, 2010

Okay, in your phonegap application in XCode, ( Without EVER having to touch the PhoneGapLib )

1) right click the plugins folder and select Add/NewFile/ObjectiveCClass
2) Call your new class : TestPlug
3) Paste the following code into the .m + .h files:

In your header file :


#import <Foundation/Foundation.h>
#import "PhoneGapCommand.h"
@interface TestPlug : PhoneGapCommand {
}
- (void)addThemUp:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end

… and in the .m file


#import "TestPlug.h"
@implementation TestPlug
- (void)addThemUp:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSUInteger argc = [arguments count];
int total = 0;
for(int n = 0; n < argc; n++)
{
total += [[ arguments objectAtIndex:n] intValue];
}
NSString* retStr = [ NSString stringWithFormat:@"alert(\"%d\");",total];
[ webView stringByEvaluatingJavaScriptFromString:retStr ];
}
@end

4) In your JS code, somewhere after deviceready, call your command like this.


PhoneGap.exec("TestPlug.addThemUp",1,2,3,4);

Build and run!

Note the structure of the PhoneGap.exec call
1) a command name : TestPlug
2) a method name : addThemUp
3) a variable length list of arguments

Next time, I'll get into passing objects via the 'options' object.

If you have a compelling plug-in you want to share, you can fork my repo for plugins and send me a pull request :

http://github.com/purplecabbage/PhoneGap-Plugins

If your plugin makes sense on multiple devices, and it is implemented with standards in mind, it may end up in PhoneGapLib.

Posted in iPhone, Objective-C, PhoneGap, XCode | 11 Comments » | Add to Delicious | Digg It

This entry was posted on Thursday, April 1st, 2010 at 8:57 am and is filed under iPhone, Objective-C, PhoneGap, XCode. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

11 Responses to “iPhoneGap Plugs”

  1. Aeron Says:

    There might be a typo between the function name in the header (doit) and in the implementation (addThemUp). But then I’m an objc newbie so maybe you’re doing something fancy I don’t understand.

    Waiting patiently for the next example with the options object…..

  2. Paul G. Hunt Says:

    This code seems to be pasted from the mailing list / Google Group. Reformatting has converted straight double quotes into curly ones, and content enclosed in angular brackets has disappeared completely.

    This may be confusing.

  3. Jesse Says:

    Thanks, I have updated the code to use a formatter.

  4. Jesse Says:

    Thanks, you are correct. I fixed the header definition. Good catch!

  5. Charlie Says:

    This is great; it really couldn’t be much simpler than this. I had my own plug-in created and working in PhoneGap in about 5 minutes. Thanks!

    I’d also love to see an example using the options dictionary… curious what it is for…

  6. Jesse Says:

    I have checked in a plugin command for iphone to support in app email.
    You can get it at http://bit.ly/aciTnE

    Grab the EmailComposer files.

    It demonstrates the passing of vars via the options object/dictionary.

  7. hiwind Says:

    fantastic. this is exactly what i needed to have imageview stay on screen until my webview and all my preloaded images were completely loaded.. from there I send a call back to c to hide imageview and then a call back to webview to proceed with the first frame of my app.

  8. Monsenyor Says:

    Where does the webView comes from in the addThemUp implementation? The compiler complains about a webView “undeclared” in that line.

    Thanks.

  9. Jesse Says:

    the webView comes from the base class PhoneGapCommand.

  10. Neil Says:

    I am trying to use the ChildBrowser plugin. I have added ChildBrowser to the Plugins folder and added the ChildBrowser.js, I install it cb = ChildBrowser.install(); I have done an alert and it says there is an instance there. The problem is when I call window.plugins.childBrowser.showWebPage(‘http://www.google.com‘); It crashes the app. The log says: attempt to insert nil value (key:ChildBrowserCommand). Do I need to add more to get the plugin working?

  11. Jesse Says:

    Make sure you add all native pieces via XCode project navigator. The js file can be added simply by placing it in the www folder, but the others need to be built by XCode.

Leave a Reply