Skip to Content

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

/wp-content/uploads/2013/06/1_226055.png

Screen Shot 2013-06-03 at 10.25.29 PM.png

Screen Shot 2013-06-03 at 10.26.06 PM.png

Finally you will have Supuserinfo.h and Supuserinfo.m file as below

Screen Shot 2013-06-03 at 10.30.02 PM.png

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

Screen Shot 2013-06-03 at 10.37.35 PM.png

Screen Shot 2013-06-03 at 10.34.40 PM.png

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.

Screen Shot 2013-06-03 at 11.08.10 PM.png

Hope this blog gives you an idea how to build navite plugins ๐Ÿ™‚

To report this post you need to login first.

9 Comments

You must be Logged on to comment or reply to a post.

  1. Armando Ibarra

    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.

    (0) 

Leave a Reply