Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
tahir_z
Contributor

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 :smile:

9 Comments
Labels in this area