Blog

Top
  • iPhoneGap Plugs

    by Jesse | Thu, Apr 1, 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 the .h file …..
    #import
    #import “PhoneGapCommand.h”
    @interface TestPlug : PhoneGapCommand {
    }
    - (void)doit:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
    @end

    // 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.