Manage offline packages

更新时间:
复制 MD 格式

Offline packages let you bundle H5 pages for local loading, avoiding the performance impact of poor network conditions. You can deliver package updates to clients through the publishing platform.

You can manage offline packages in the following ways:

Prerequisites

Ensure that NebulamPaaSBiz.framework is integrated into the client project after adding the SDK.

Generate an offline package

To generate an .amr offline package, build a frontend .zip package and then generate the .amr package online. For more information, see Generate an offline package.

Load an offline package

You can load an offline package in two ways, depending on whether the package is preset on the client:

Preset an offline package

Pages such as the home page or logon page must load quickly regardless of network conditions. You can package these resources into an offline package and preset it in the project so that they load even when the application is offline.

Perform the following steps:

  1. Create a separate bundle, such as DemoCustomPresetApps.bundle. Add the .amr offline package and the h5_json.json file downloaded from the publishing platform to this bundle.111

    Note

    The publishing platform currently supports downloading the h5_json.json configuration file for only a single offline package. If you are presetting multiple offline packages, you must manually merge them into the data array of the JSON file.

  2. When initializing the container, call the initNebulaWithCustomPresetApplistPath method and set the preset offline package path to the bundle that you created in the previous step.

     - (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
     {
         // Initialize rpc
         [MPRpcInterface initRpc];
    
         // Initialize the container
         // [MPNebulaAdapterInterface initNebula];
    
         // Set the custom 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];
     }
  3. Similar to loading a non-preset offline package, call the method provided by the Nebula container to load the offline package when you enter the corresponding page.

     - (void)openPresetPackage {
         [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithNebulaApp:@{@"appId":@"20180910"}];
     }

Load a remote offline package

You can also dynamically publish an offline package through the publishing platform and load it directly on the client, instead of presetting it. This prevents the client package from becoming oversized.

Perform the following steps:

  1. After the application starts, preload the package information and download the offline package. This prevents a blank screen from appearing when the package is opened.

    • Interface methods

        @interface MPNebulaAdapterInterface : NSObject
      
        /**
         *  Request for a single application
         *
         *  @param params   The list of requests in {appid:version} format. You can pass multiple app IDs. The version number can have up to four segments, such as 1.0.0.1. If you do not specify a version, pass an empty value to use the latest version. Fuzzy matching is supported. For example, '*' matches the latest version, and '1.*' matches the latest version that starts with 1.
         *  @param finish   Completion callback
         */
        - (void)requestNebulaAppsWithParams:(NSDictionary *)params finish:(NAMRequestFinish)finish;
      
        @end
  2. After the client is configured, you can deliver an offline package through the publishing platform. For more information, see Real-time Release > Offline Package Management > Publish an offline package.

  3. When you enter the corresponding page, call the method provided by the Nebula container to load the offline package. The page will then use the offline package that was delivered from the publishing platform.

    • Sample code

        - (void)openPresetPackage {
        [[MPNebulaAdapterInterface shareInstance] startH5ViewControllerWithNebulaApp:@{@"appId":@"20180910"}];
        }
    • Interface method

        @interface MPNebulaAdapterInterface : NSObject
      
        /**
         *  Creates an H5 container based on the provided offline package information and automatically pushes to open it.
         *
         *  @param params The startup parameters for the H5 container. The appId parameter is required. For other optional parameters, see the document at https://tech.antfin.com/docs/2/85001.
         *
         */
        - (void)startH5ViewControllerWithNebulaApp:(NSDictionary *)params;
      
        @end

Use a global resource package

When multiple H5 applications share the same resources, such as ReactJS framework code, you can place those resources into a global resource package to reduce the size of each application. Configure the global offline package in the afterDidFinishLaunchingWithOptions method, as shown in the following sample code. In this sample, 77777777 is the app ID of the global resource package.

The nebulaCommonResourceAppList property specifies which offline package the H5 container uses as a global resource package. If you do not configure this ID, the package will not take effect, even if it is built-in.

```
- (void)application:(UIApplication *)application afterDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [MPNebulaAdapterInterface shareInstance].nebulaCommonResourceAppList = @[@"77777777"];// Set the global resource package
}
```

To improve page loading speed, preset the global resource package. You can still deliver updates later through the H5 application backend.

Dynamically update an offline package

mPaaS provides dynamic update capabilities. You can deliver a new version of an offline package through the publishing platform to update the corresponding page on the client. For more information, see Real-time Release > Offline Package Management > Publish an offline package.

Related links