How to create develop user component with Native Library

This section provides the steps needed to create NEST components with Native Library

“Native Library refers to a Function library developed by the user or published by a 3rd party company. NEST can only load source codes library from the following mobile platforms (iOS, Android, Windows phone).

Please first refer to the “User application Component descriptor” then follow the four steps below

  1. How to declare JavaScript component (Component Indicator).
  2. How to structure native library
  3. How to develop plugin class called by JavaScript component.
  4. How to set JavaScript component call method.

  1. Component Indicator
    • plugin descriptor

      This section describes Native Library Info.

      Declaration Method

      plugin : {
      	include : [{
      		name : "Facebook",
      		URL : "http://www.sample.com/fb/Facebook.zip",
      		warning : {
      			WindowsPhone : "Facebook is not supported yet on this platform",
      			HTML5 : "Facebook is not supported yet on this platform",
      		},
      	}],
      },

      Element Description

      Element Description
      name Defines Native Library name
      URL Define Native Library download location
      warning Define warning message for unsupported platforms
  2. Native Library File Structure
    Directory/File Description
    assets build plugins <plugin> android Android Directory for Native Library
    iOS iOS Directory for Native Library
    WindowsPhone Windows Phone Directory for Native Library
    Simulator Simulator Directory for Native Library
    descriptor.xml File defining Native Library Build Method

    descriptor.xml File Description

    This file defines the method involved in merging the NEST project file with Native Library

    Element: plugin

    Sub-Element Description
    builder Must define a source that is applicable to the platform.

    Element: builder

    Sub-Element Description
    file Define file that will be updated in the project. (optional)
    copy Define additional files that must be copied (optional)
    resource Defines additional android resource files (optional)

    Element: file

    Sub-Element Description
    append   Define source code to be added to file (optional)
    placement

    Define additional source code location

    ※ Generally placement is displayed as comment in the NEST build source code

    Configuration Example

    AndroidManifest.xml <!-- activity -->
    <!-- gcm-service -->
    AppDelegate.m //@INCL/#import
    //@INCL/didFinishLaunchingWithOptions
    //@INCL/#body
    content Define additional source code

    Element: copy

    Sub-Element Description
    source Define original directory route to be copied
    target Define target directory route to be copied (optional)
    If there are no target directory routes, root directory is used. (optional)

    Element: resource

    Sub-Element Description
    directory Define Android resource directory
    extra-package Define Android resource package name

    Configuration Example

    <?xml version="1.0" encoding="UTF-8"?>
    <plugin>
    	<builder target="Android">
    		<file name="src/main/AndroidManifest.xml">
    			<append>
    				<placement><![CDATA[<!-- activity -->]]></placement>
    				<content><![CDATA[
            <activity android:name="com.facebook.FacebookActivity"
                android:configChanges=
                    "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
                android:theme="@android:style/Theme.Translucent.NoTitleBar"
                android:label="@string/app_name">
            </activity>
    ]]></content>
    			</append>
    		</file>
    
    		<copy>
    			<source>libs</source>
    		</copy>
    		<copy>
    			<source>src</source>
    		</copy>
    		
    		<resource>
    			<directory>res</directory>
    			<extra-package>com.facebook</extra-package>
    		</resource>
    	</builder>
    	
    	<builder target="iOS">
    		…
    	</builder>
    
    </plugin>
  3. How to develop plugin class called by JavaScript component

    Native Library must be supported by each platform

    Class Naming Rule
    Class name must be submitted in lower case.
    1. How to develop iOS Plugin Class

      iOS Plugin Class on the iOS Native Library must inherit the AbstractPlugin class

      Abstract ClassAbstractPlugin
      Locationassets/build/ios/NESTApp/NESTApp/AbstractPlugin.h

      Property

      Type Name Description
      UIWebView* webView UIWebView
      UIViewController* viewController UIViewController

      Method

      (void) initialize;
      Initiate Plugin
      - (void) execute: (NSArray *) array;
      Main method for running Plugin. This method must be overridden for implementing the function
      array: Argument passed from JavaScript
      - (void) jsCallback: (NSInteger) code withObject: (id) result;
      Function transmitting for JavaScript component result
      code : Return code
      result : NSDictionary Object is automatically converted to JSON Object type and transmitted to JavaScript
      - (void) jsCallback: (NSInteger) code withMessage: (NSString *) message;
      Function transmitting for JavaScript component result
      code : Return code
      message : Return message

      Example) sendobject.h

      #import <Foundation/Foundation.h>
      #import <UIKit/UIKit.h>
      
      @interface sendobject : AbstractPlugin {
      }
      @end
      

      Example) sendobject.c

      #import "AbstractPlugin.h"
      #import "sendobject.h"
      
      @implementation sendobject
      
      - (void) execute : (NSArray *) a
      {
          NSLog(@"me --> %@", @"sendobject.execute");
          NSString *text = a[0];
          NSArray *activityItems = [NSArray arrayWithObjects:text, nil];
          
          UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
          if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
              [[self viewController] presentViewController:controller animated:YES completion:^{}];
          } else {  //if iPad
              CGSize sz = [self webView].frame.size;
              UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
              [popup presentPopoverFromRect:CGRectMake(sz.width/2, sz.height/4, 0, 0) inView:[self webView] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
          }
          
          [self jsCallback:fKey withCode:0 withObject:nil];
      }
      
      @end
      
    2. How to develop Android Plugin Class

      AndroidPlugin Class on the Android Native Library must inherit the com.nestpia.nest.plugins.AbstractPlugin class

      Abstract ClassAbstractPlugin
      Locationassets/build/android/NEST/src/main/java/com/nestpia/nest/plugins/AbstractPlugin.java
      Constraint

      Plugin class that have inherited AbstractPlugin class must be named with com.nestpia.nest.plugins to be packaged.

      This constraint was designed with reflection methodology for when no additional configurations are needed while loading. If the package name is different, when NEST is loading the plugin class package will not be recognized.

      Property

      Type Name Description
      android.content.Context context context
      android.webkit.WebView view view

      Method

      void init()
      Initiate Plugin
      - public abstract void execute(String[] array)
      Main method for running Plugin. This method must be overridden for implementing the function
      array: Argument passed from JavaScript
      protected void callbackWithObject(int code, Object result)
      Function transmitting for JavaScript component result
      code : Return code
      result : Object is automatically converted to JSON Object type and transmitted to JavaScript
      protected void callbackWithMessage (int code, String message)
      Function transmitting for JavaScript component result
      code : Return code
      message : Return message
    3. How to develop Windows Plugin Class

      Windows Phone Plugin Class on the Windows Phone Native Library must inherit the NEST.plugins.AbstractPlugin class

      Abstract ClassAbstractPlugin
      Locationassets/build/windowsphone/NESTs/AbstractPlugin.cs
      ConstraintPlugin class that have inherited AbstractPlugin class must be named with NEST.plugins to be packaged.
      This constraint was designed with reflection methodology for when no additional configurations are needed while loading. If the package name is different, when NEST is loading the plugin class package will not be recognized.

      Property

      Type Name Description
      Microsoft.Phone.Controls.WebBrowser browser browser
      Microsoft.Phone.Controls.PhoneApplicationPage page page

      Method

      void init()
      Initiate Plugin
      public abstract void execute(String[] data)
      Main method for running Plugin. This method must be overridden for implementing the function
      array: Argument passed from JavaScript
      protected void callbackWithObject(int code, object result)
      Function transmitting for JavaScript component result
      code : Return code
      result : NSDictionary Object is automatically converted to JSON Object type and transmitted to JavaScript
      protected void callbackWithObject (int code, string message)
      Function transmitting for JavaScript component result
      code : Return code
      message : Return message
    4. How to develop Simulator Plugin Class

      Simulator Plugin Class on the Simulator Native Library must inherit the com.nestpia.simulator.plugins. AbstractPlugin class

      Abstract ClassAbstractPlugin
      Locationassets/build/windowsphone/NESTs/AbstractPlugin.cs
      ConstraintPlugin class that have inherited AbstractPlugin class must be named with NEST.plugins to be packaged.
      This constraint was designed with reflection methodology for when no additional configurations are needed while loading. If the package name is different, when NEST is loading the plugin class package will not be recognized.

      Property

      Type Name Description
      org.eclipse.swt.browser.Browser browser browser

      Method

      void init()
      Initiate Plugin
      public abstract void execute(String[] data)
      Main method for running Plugin. This method must be overridden for implementing the function
      array: Argument passed from JavaScript
      protected void callbackWithObject(int code, object result)
      Function transmitting for JavaScript component result
      code : Return code
      result : NSDictionary Object is automatically converted to JSON Object type and transmitted to JavaScript
      protected void callbackWithObject (int code, string message)
      Function transmitting for JavaScript component result
      code : Return code
      message : Return message
      • The developed class must be published as jar package type
      • The sample code for the simulator can be found on simulator-sample.zip
  4. How to set JavaScript component call method

    Method

    NEST.xlib.execute(classname, callback, argv[])
    Call Plugin class on Native Library
    classname: Calls class name
    callback: Callback function for receiving return
    argv: Argument passed from Plugin Class

    Example

    clazz.prototype.login = function() {
    	 var me = this;
    	 if (NEST.xlib.execute) { // native library Check for Native library availability 
    		 var fk = NEST.xlib.callback(function(err, data) {
    		 	 if (err === 0) {
    	 		 	 me.triggerData(err, data);
    		 	 } else {
    		 	 	 me.loginInternal();
    		 	 }
    		 }); // callback function
    		 NEST.xlib.execute("facebook.status", fk, undefined);
    	 } else {
    		 setTimeout(function() { // Reload native library if the loading time is delayed. 
    	 		 if (NEST.xlib.execute) {
    	 	 		 me.login();
    		 	 } else {
    			     me.__error = "Unsupported function";
    			     me.trigger("onError");
    		 	 }
    		 }, 500);
    	 }
    }