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.
April 21st, 2010 at 6:07 pm
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…..
May 1st, 2010 at 4:00 pm
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.
May 1st, 2010 at 4:43 pm
Thanks, I have updated the code to use a formatter.
May 1st, 2010 at 4:44 pm
Thanks, you are correct. I fixed the header definition. Good catch!
May 9th, 2010 at 1:21 am
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…
May 9th, 2010 at 1:28 am
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.
November 5th, 2010 at 9:47 pm
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.
November 18th, 2010 at 9:13 am
Where does the webView comes from in the addThemUp implementation? The compiler complains about a webView “undeclared” in that line.
Thanks.
March 7th, 2011 at 2:25 pm
the webView comes from the base class PhoneGapCommand.
April 5th, 2011 at 6:02 am
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?
May 4th, 2011 at 12:34 pm
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.