All Products
Document Center

Quick start

Last Updated: Mar 24, 2021

You can use the HTML5 container and offline package to implement the following functions: container initialization and invocation, bi-directional communication between HTML5 and Native, loading and use of offline packages, integration of the automatic tracking ability of the Nebula container, and check of tracking data.

Before you begin

The project already gets access to mPaaS. For more information, see the following content: Access based on an existing project and use of CocoaPods

Add the SDK

Use the cocoapods-mPaaS plug-in. This method is applicable to the access mode based on an existing project and use of CocoaPods.

  1. In the Podfile file, use mPaaS_pod "mPaaS_Nebula" to add the dependencies of the HTML5 container component.
  2. Run pod install to complete the access of HTML5 container.

Use the SDK

This topic describes how to use the HTML5 container SDK in baseline version 10.1.60 or later versions with the official demo of the HTML5 container and offline package.

Initialize the container

Start the container

  • To use the Nebula container, you need to call the SDK API to initialize the container after the program is started. Initiate the container in - (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions of DTFrameworkInterface.

    1. - (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    2. {
    3. // Initialize the container.
    4. [MPNebulaAdapterInterface initNebula];
    5. }
  • If you need to use the preset offline package, customize JSAPI, and plug-in functions, replace the initNebula API in the previous code with the initNebulaWith API in the following code. Then pass in the corresponding parameters to initialize the container.

    • presetApplistPath: custom path of the preset offline package information.
    • appPackagePath: custom path of the preset offline package.
    • pluginsJsapisPath: storage path of custom JSAPI and custom plug-in files.

      1. - (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      2. {
      3. // Initialize the container.
      4. NSString *presetApplistPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"MPCustomPresetApps.bundle/h5_json.json"] ofType:nil];
      5. NSString *appPackagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"MPCustomPresetApps.bundle"] ofType:nil];
      6. NSString *pluginsJsapisPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Poseidon-UserDefine-Extra-Config.plist"] ofType:nil];
      7. [MPNebulaAdapterInterface initNebulaWithCustomPresetApplistPath:presetApplistPath customPresetAppPackagePath:appPackagePath customPluginsJsapisPath:pluginsJsapisPath];
      8. }
Note: initNebula and initNebulaWithCustomPresetApplistPath are two methods for container initialization. Do not call both of them at the same time.
  • Configure the time interval of requests for Mini program packages. mPaaS supports both global and individual configuration of the time interval.
    • Global configuration: You can use the following code to set the update request interval of offline packages or Mini programs during container initialization.
      1. [MPNebulaAdapterInterface shareInstance].nebulaUpdateReqRate = 7200;
      Where, 7200 indicates the global update request interval. 7200 is the default value. The unit is second. You can change this value to set the global interval of requests for offline packages. The value range is 0 to 86400 seconds (that is, 0 to 24 hours). 0 indicates that there is no limit on request interval.
    • Individual configuration: The time interval of requests is only set for the current Mini program package. You can go to Add Offline Package > Extended Information in the console and then enter {"asyncReqRate":"1800"} to set the time interval of requests. For details, see Extended information in Create HTML5 offline package.

Customize the container

  • You can set the attributes in MPNebulaAdapterInterface to customize the container configuration when necessary. The configuration must be implemented in - (void)application:(UIApplication *)application afterDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions of DTFrameworkInterface. Otherwise, the configuration will be overridden by the default configuration of the container.

    1. - (void)application:(UIApplication *)application afterDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    2. {
    3. // Customize the container.
    4. [MPNebulaAdapterInterface shareInstance].nebulaVeiwControllerClass = [MPH5WebViewController class];
    5. [MPNebulaAdapterInterface shareInstance].nebulaNeedVerify = NO;
    6. [MPNebulaAdapterInterface shareInstance].nebulaUserAgent = @"mPaaS/Portal";
    7. [MPNebulaAdapterInterface shareInstance].nebulaCommonResourceAppList = @[@"77777777"];
    8. }
  • The following table describes the attributes:

Attribute Description Remark
nebulaVeiwControllerClass Base class of HTML5 page The default is H5WebViewController. You can set this API directly if it is required to specify base classes for all HTML5 pages. Caution: All base classes must be derived from H5WebViewController by inheritance.
nebulaWebViewClass Sets base class of WebView > 10.1.60: The default is H5WKWebView. A custom WebView must be derived from H5WKWebView by inheritance.
= 10.1.60: customization not supported.
nebulaUseWKArbitrary Sets whether to use WKWebView to load the offline package page > 10.1.60: The default is YES.
= 10.1.60: The default is NO.
nebulaUserAgent Sets UserAgent of the application The UserAgent is attached to the default UA of the container as a suffix.
nebulaNeedVerify Sets whether signature verification is required. The default is YES. Set the attribute value to NO if no private key file is uploaded when you Configure offline package. Otherwise, the offline package loading will fail.
nebulaPublicKeyPath Path of the public key that is used for signature verification of offline packages It is the path of the public key corresponding to the private key uploaded when you configure offline package.
nebulaCommonResourceAppList The app ID list of the common resource package -
errorHtmlPath Path of the HTML error page that is displayed when HTML5 page loading fails Read from MPNebulaAdapter.bundle/error.html by default.
configDelegate Sets the custom switch delegate. Supports global modification of the default container switch.

Update an offline package

Make a request for full information of offline packages after startup and check whether update packages are available on the server. To prevent impact on the startup speed, we recommend that you request calling after (void)application:(UIApplication \*)application afterDidFinishLaunchingWithOptions:(NSDictionary \*)launchOptions.

  1. - (void)application:(UIApplication *)application afterDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. // Customize the container.
  4. [MPNebulaAdapterInterface shareInstance].nebulaVeiwControllerClass = [MPH5WebViewController class];
  5. [MPNebulaAdapterInterface shareInstance].nebulaNeedVerify = NO;
  6. [MPNebulaAdapterInterface shareInstance].nebulaUserAgent = @"mPaaS/Portal";
  7. [MPNebulaAdapterInterface shareInstance].nebulaCommonResourceAppList = @[@"77777777"];
  8. // Full update of offline packages
  9. [[MPNebulaAdapterInterface shareInstance] requestAllNebulaApps:^(NSDictionary *data, NSError *error) {
  10. NSLog(@"");
  11. }];
  12. }

The following figure shows the effect after the initialization is complete:

after initialization

Configuration not managed by the framework

If the lifecycle of your app is managed by a custom delegate instead of the mPaaS framework, additional configuration is required, as shown in the following figure. If your app is managed by the mPaaS framework, skip this step.


Start the mPaaS framework

In the didFinishLaunchingWithOptions method of the current application, call [[DTFrameworkInterface sharedInstance] manualInitMpaasFrameworkWithApplication:application launchOptions:launchOptions]; to start the mPaaS framework.


Note: To ensure successful startup of the framework, implement the call after the initialization of window and navigation of the current application is complete.
Create a bootloader

Create a subclass derived from DTBootLoader, rewrite the createWindow and createNavigationController methods, and return the window and navigationControlle of the current application.

  • Set window: keyWindow of the current application.
  • Set navigationController: rootviewcontroller of keyWindow of the current application, which must be derived from DFNavigationController.



Specify a bootloader

Rewrite the method in category of DTFrameworkInterface, specify the bootloader of the current application, and hide the default window and launcher applications of the mPaaS framework.


Invoke a container

After container initialization, you can evoke an HTML5 container. The following describes three invoking methods:

  • Create an HTML5 container based on an online URL or local HTML file. Refer to the following code example:

    1. // Open an online URL.
    2. [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithParams:@{@"url": @""}];
    3. // Open a local HTML page.
    4. NSString *path = [[NSBundle mainBundle].bundlePath stringByAppendingFormat:@"/%@/%@", @"MPH5Demo.bundle", @"H52Native.html"];
    5. if ([path length] > 0) {
    6. [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithParams:@{@"url": path}];
    7. }
  • Create an HTML5 container based on the received offline package information and use automatic push to open the container. Refer to the following code example:

    1. [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithNebulaApp:@{@"appId":@"90000000"}];
  • Create an HTML5 container based on the received offline package information and return the created HTML5 container instance. This is usually used on a tab of homepage. Refer to the following code example:

    1. [[MPNebulaAdapterInterface shareInstance] createH5ViewControllerWithNebulaApp:@{@"appId":@"90000000"}];

Implement bidirectional communication between HTML5 and native apps.

You can call JSAPIs and listen to certain events to implement bidirectional communication between HTML5 and native apps.

Call the native function on an HTML5 page

You can call JSAPIs to enable HTML5 to communicate with Native.

For details about JSAPI supported by the Nebula container and related parameters, see Built-in JSAPI.


Call a JSAPI pushWindow to load a new page when a button is tapped on an HTML5 page.

  1.'pushWindow', {
  2. url: '',
  3. param: {
  4. readTitle: true,
  5. defaultTitle: true,
  6. // ...
  7. }
  8. }, function(data) {alert('Call result'+JSON.stringify(data)); });
AlipayJSBridge description

AlipayJSBridge AlipayJSBridge is a JSBridge automatically injected by the Nebula container. After the implementation of Window.onload , the container will generate a global variable AlipayJSBridge and trigger an AlipayJSBridgeReady event.AlipayJSBridge injection is an asynchronous process. Therefore, listen to the AlipayJSBridgeReady event before calling an API.

Refer to the following code example:

  1. <h1>bridge API usage instruction</h1>
  2. <script>
  3. function ready(callback) {
  4. if (window.AlipayJSBridge) {
  5. callback && callback();
  6. } else {
  7. document.addEventListener('AlipayJSBridgeReady', callback, false);
  8. }
  9. }
  10. ready(function(){
  11. alert('bridge ready');
  12. });
  13. </script>

Call HTML5 on a native page

You can listen to certain events to enable communication between the native and HTML5. For information about events supported by the Nebula container, see Extended event list.

  1. document.addEventListener('back', function (e) {
  2. if(confirm('back intercepted. Are you sure you want to return?')) {
  3. // do something;
  4. }
  5. }, false);

In addition to default events supported by the Nebula container, you can use the following method on the Native end to define an event that the front end can listen.

  1. // self: VC where the current HTML5 page is located
  2. // data: parameter passed to the front end by native
  3. // callBack: callback after the front end receives the event
  4. [self callHandler:@"customEvent" data:@{@"key":@"value"} responseCallback:^(id responseData) {
  5. NSLog(@"callback after the front end receives the event: %@", responseData);
  6. }];

Expand Nebula container capabilities

If the basic bidirectional communication between HTML5 pages provided by the Nebula container is insufficient, you can expand the capabilities of Nebula.

  • JSAPI: To call the native function on a page, for example, to display an ActionSheet or contacts dialog box, you need to expand a JSAPI. JSAPI allows you to use the handler method to add a native feature for an HTML5 page to implement a specific function. For detailed instructions, see Customize a JSAPI.

  • Plugin: To finish certain events (such as recording tracking and modifying returned data) at a certain time point (such as when entering a page or receiving a request), you need to develop a plug-in. After subscribing to corresponding events, the plug-in can process data carried in the events in the handler. For detailed instructions, see Customize a plug-in.

Load an offline package

The traditional online HTML5 technology is subject to actual network environment, which may compromise the performance of HTML5 pages. To minimize network impact on HTML5 page loading, you can encapsulate different services as an offline package and deliver it to the client through the release platform to update client resources. For more information, see Introduction to offline packages and Use offline packages.

HTML5 container tracking

When an HTML5 page is loaded, the Nebula container will automatically monitor the loading performance and capture related behavior data and exception data. For more information, see HTML5 container tracking.