All Products
Document Center

Mobile Platform as a Service:Custom JSAPI

Last Updated:Aug 15, 2022

To call the native function on a page, for example, to display an ActionSheet or contacts dialog box, you need to expand a JavaScript API (JSAPI). With the JSAPI, you can add an entry to call the native function on an HTML5 page. Certain functions are implemented in native mode by implementing the handler method in the custom JSAPI class.

The HTML5 container component has the following capabilities:

  • Provides abundant embedded JSAPIs to implement functions such as push, pop, and title setup. For more information, see Built-in JSAPI.

  • Allows you to customize JavaScript API (JSAPI) and plug-in to satisfy your business expansion needs.

This topic describes how to use a demo of HTML5 container and offline package to customize a plug-in that modifies the navigation bar when an HTML5 page is loaded.

About this task

You can customize a JSAPI by using two methods:

  • Plist registration

  • Code registration


Plist registration

  1. Create a JSAPI class:

    • Naming format: For consistency with the names of plug-ins provided by the container by default, the created JSAPI name begins with XXJsApi4, where XX is a custom prefix.

    • Base class: All JSAPIs are derived from PSDJsApiHandler.

    • Basic method: Rewrite method -(void)handler:context:callback: in the .m file. When this JSAPI is called in the frontend, the call request will be forwarded to this method.

    • The following describes the parameters in this method:




      The parameter passed in when an HTML5 page calls this JSAPI.


      The context of the current HTML5 page. For details, see PSDContext.h.


      The callback method after the JSAPI call is complete. This method returns a dictionary as the calling result to the HTML5 page.

      Refer to the following code example:

         #import <NebulaPoseidon/NebulaPoseidon.h>
         @interface MPJsApiHandler4OpenSms : PSDJsApiHandler
         @implementation MPJsApiHandler4OpenSms
         - (void)handler:(NSDictionary *)data context:(PSDContext *)context callback:(PSDJsApiResponseCallbackBlock)callback
         [super handler:data context:context callback:callback];
         // Open the SMS.
         NSURL *url = [NSURL URLWithString:@"sms://xxx"];
         BOOL reasult = [[UIApplication sharedApplication] openURL:url];
  2. Register the JSAPI. Register this JSAPI in the custom Plist file.

    • Create a PLIST file for unified management of custom JSAPI and plug-ins. You can download the template file and add this file to your project.

    • In the JsApis array, register the JSAPI class created in the previous step.

    • The registered JSAPI is of the dictionary type and includes two items.


         <td>The name of the JSAPI called on the HTML5 page. <div class="alert alert-notes">**Caution:** Attach a prefix to the name of a custom JSAPI to distinguish the custom JSAPI from the built-in JSAPI of the container. The purpose is to prevent unavailability due to interaction between the custom JSAPI and the built-in JSAPI.</div></td>

         <td>The name of the created JSAPI class.</td>

      • Specify the path of the custom PLIST file during container configuration initialization.

      Refer to the following code example:

         - (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
             // Initialize the container.
             //    [MPNebulaAdapterInterface initNebula];
             // Specify JSAPI path and preset offline package information.
             NSString *presetApplistPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"DemoCustomPresetApps.bundle/h5_json.json"] ofType:nil];
             NSString *appPackagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"DemoCustomPresetApps.bundle"] ofType:nil];
             NSString *pluginsJsapisPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"DemoCustomPlugins.bundle/Poseidon-UserDefine-Extra-Config.plist"] ofType:nil];
             [MPNebulaAdapterInterface initNebulaWithCustomPresetApplistPath:presetApplistPath customPresetAppPackagePath:appPackagePath customPluginsJsapisPath:pluginsJsapisPath]

Code registration

In addition to customizing a JSAPI using the Plist, you can register a custom JSAPI by calling the interface method provided by the Nebula container.

  1. Create a plug-in. See Customize a plug-in.

  2. Implement the addJSApis method in the plug-in.

     - (void)addJSApis
         [super addJSApis];
         // Register JSAPI by code.
         PSDJsApi *jsApi4DemoTest2 = [PSDJsApi jsApi:@"demoTest2"
                                               handler:^(NSDictionary *data, PSDContext *context, PSDJsApiResponseCallbackBlock responseCallbackBlock) {
                                                   responseCallbackBlock(@{@"result":@"jsapi-demoTest2 The result of calling Native"});
         [self registerJsApi2Target:jsApi4DemoTest2];
    • The following table describes the parameters required for registration.




      The name of the JSAPI called on the HTML5 page.


      JSAPI handler function, which is the same as the handler method in the PLIST registration mode.


      Specifies whether parameters are checked. Set this parameter to NO.


      Specifies whether the JSAPI is a private JSAPI. Set this parameter to NO.


      Scope of action. Set this parameter to self.scope.

For details, see the implementation of the MPPlugin4TitleView class in the code example.

What to do next

  1. Call the custom JSAPI on an HTML5 page.

  2. Add a breakpoint in the handler method and check whether the parameters provided by the HTML5 page meet the expectations.

  3. Check whether the results returned by the HTML5 page meet the expectations.