This blog presents how to write your own native plugin for Phonegap(also known as Apache Cordova) and implement on HWC container application. Many of you are already using phonegap plugins on hwc application for getting device name, platform, version or etc. as your needs.
All Phonegap api consist of two parts, a javascript based can be accessed within your hybird app project and the corresponding native class for performing operation in native code. The javascript class invokes the native code using the Cordova.exec() function. When it invokes Cordova.exec() it can pass in a result handler function, an error handler function, and an array of parameters to be passed into native code. Phonegap will manage the JavaScript-to-native communication.
Useful link
Building Phonegap native plugin for HWC container – Android
Let’s start building first plugin
In this blog we will get basically, hwc container username that you register on scc.
The javascript class
In SUP workspace on your hwc project create a javascript file name it as Supuserinfo. Add the following code
var SupuserinfoAccess = {
devicename: function(types, success, fail) {
return Cordova.exec(success, fail, “Supuserinfo”, “getUserinfo”, types);
}
};
The Native Class
Get the source code of HWC IOS container application on XCODE and create a new file name it as Supuserinfo with subclass of CDVPlugin
Finally you will have Supuserinfo.h and Supuserinfo.m file as below
In Supuserinfo.h file add the getuserinfo method
@interface Supuserinfo : CDVPlugin
-(void)getUserinfo:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
In Supuserinfo.m file add the following code
#define kNSiPhoneUserNamePref @”username_preference”
@implementation Supuserinfo
– (void) getUserinfo:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSString *callbackid = [arguments objectAtIndex:0];
NSString *stringreturned = [arguments objectAtIndex:1];
CDVPluginResult* pluginResult;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
stringreturned = @””;
if (userDefaults != nil)
{
stringreturned = [userDefaults stringForKey:kNSiPhoneUserNamePref];
NSLog(@”bundleUserName : %@”,stringreturned);
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString: stringreturned];
[self writeJavascript:[pluginResult toSuccessCallbackString:callbackid]];
}
Add your plugin to the Cordova.plist file
Clean project and build again.
Calling our plugin in Hybrid app
Go to the your Hybrid app project and in custom.js file call our plugin to test.
function customAfterWorkflowLoad() {
document.addEventListener(“deviceready”, onDeviceReady, true);
}
function onDeviceReady() {
SupuserinfoAccess.devicename([“x”],onSuccess,onFailure);
}
function onSuccess(name){
alert(name);
}
function onFailure(error){
}
Deploy your project to the modified HWC container application and you will get registered username as below.
Hope this blog gives you an idea how to build navite plugins 🙂
This really helps Tahir. Kindly share the document for Android as well.
– Suraj
I have posted for android
Building Phonegap native plugin for HWC container – Android
Thanks for the wonderful blog!
thanks for sharing 🙂
It’s really helpful article..Thanks for sharing,I also read the document for Android and that is helpful too.. 🙂
Hi Tahir,
really it’s very helpful blog.
Cheers,
Syam.
Really Helpful!! Thanks for sharing.. Regards, Brijesh
now.. i will try this..
i have checked with android one.. and that worked perfectly.
thanks for sharing 🙂
Regards,
Jitendra
Hi:
I already did the steps you were telling me to do but I do not have too much knowledge on the XCode.
What should I do after “Clean project and build again?”
Will an ipa document be generated or do I package the directory?
Could you help me please?
Regards.
Armando Ibarra.