Skip to Content
Author's profile photo Tahir Öz

Building Phonegap native plugin for HWC container – IOS

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 🙂

Assigned Tags

      9 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      This really helps Tahir. Kindly share the document for Android as well.

      - Suraj

      Author's profile photo Tahir Öz
      Tahir Öz
      Blog Post Author
      Author's profile photo Former Member
      Former Member

      Thanks for the wonderful blog!

      Author's profile photo Former Member
      Former Member

      thanks for sharing 🙂

      Author's profile photo Former Member
      Former Member

      It's really helpful article..Thanks for sharing,I also read the document for Android and that is helpful too.. 🙂

      Author's profile photo Syam Babu
      Syam Babu

      Hi Tahir,

      really it's very helpful blog.

      Cheers,

      Syam.

      Author's profile photo Brijesh Pati
      Brijesh Pati

      Really Helpful!! Thanks for sharing..  Regards, Brijesh

      Author's profile photo Jitendra Kansal
      Jitendra Kansal

      now.. i will try this..

      i have checked with android one.. and that worked perfectly.

      thanks for sharing  🙂

      Regards,

      Jitendra

      Author's profile photo Former Member
      Former Member

      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.