All Products
Search
Document Center

Preset an iOS mini program in the client

Last Updated: Feb 04, 2021

The traditional Mini Program technology can be easily affected by the network environment. Therefore, you may fail to pull the Mini Program package when the network condition is poor. However, you can avoid this problem by presetting the Mini Program. This topic introduces the principle and implementation process of presetting a Mini Program.

About presetting a Mini Program

Presetting a Mini Program is to pack the static resources of the Mini Program such as rendering, logic, and configuration into a compressed package. Then, the client downloads the Mini Program package to the local in advance and loads the resources locally. Presetting the Mini Program can minimize the impact of the network environment on the mPaaS Mini Program page. The benefits of using the preset package are as follows.

  • Improve the user experience
    By embedding the static resources of the page into the App through the preset package and releasing the resources with the App, when you open the App for the first time, you can directly use the resources without relying on the network environment to download them.
  • Implement dynamic update
    When a new version or emergency release is about to be launched for a Mini Program, you can conduct iterative development in the mini program IDE and release the new version in the mPaaS console. The Mini Program SDK integrated in the client will then automatically update the Mini Program to the latest version. This release method does not require App Store review, allowing users to receive updates in a timely manner.

Prerequisites

Procedure

  1. Preset the mini program package.
    1. In the mPaaS console, release the Mini Program package and download the AMR and configuration files.
      download
    2. Create an independent bundle, such as DemoCustomPresetApps.bundle, and add the AMR offline package and the h5_json.json file downloaded from the release platform to this bundle.
      Note: Currently, the release platform only supports downloading the h5_json.json configuration file of a single offline package. To preset multiple mini program package, the data in different h5_json.json files need to be manually merged into one configuration file.
      bundle
    3. When initializing the mini program, set the offline path of the preset mini program offline package in the initNebulaWithCustomPresetApplistPath interface to the bundle created in the previous step.
           
      1. - (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      2. {
      3. // Initialize rpc
      4. [MPRpcInterface initRpc];
      5. // Initialize the container
      6. // [MPNebulaAdapterInterface initNebula];
      7. // Custom JSAPI path and preset mini program package information
      8. NSString *presetApplistPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"DemoCustomPresetApps.bundle/h5_json.json"] ofType:nil];
      9. NSString *appPackagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"DemoCustomPresetApps.bundle"] ofType:nil];
      10. NSString *pluginsJsapisPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"DemoCustomPlugins.bundle/Poseidon-UserDefine-Extra-Config.plist"] ofType:nil];
      11. [MPNebulaAdapterInterface initNebulaWithCustomPresetApplistPath:presetApplistPath customPresetAppPackagePath:appPackagePath customPluginsJsapisPath:pluginsJsapisPath];
      12. }
  2. Start the Mini Program.
    Similar to the non-preset mini programs, when entering the corresponding page, the interface method provided by the Nebula container is called to load the mini program.
       
    1. [MPNebulaAdapterInterface startTinyAppWithId:@"2020121720201217" params:nil];
  3. Update the Mini Program.
    By default, every time you open the App, the Mini Program SDK attempts to check if a newer version is available. The check interval is restricted to 30 minutes by default to minimize the stress on the server. To instantly check for the latest available version, call the following code to request an update. Generally, you can call the code after the App starts or the user logs in.
       
    1. -(void)application:(UIApplication *)application afterDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    2. // Fully update the local mini program package information
    3. [[MPNebulaAdapterInterface shareInstance] requestAllNebulaApps:^(NSDictionary *data, NSError *error) {
    4. NSLog(@"[mpaas] nebula rpc data :%@", data);
    5. }];
    6. }
  4. Check the security signature.
    The Mini Program has a signature verification mechanism to prevent malicious programs from tampering with the mini program package downloaded to the device. To enable this mechanism, call the mini program API to configure verification parameters.
    Note:
    • Be sure to call the MPNebulaAdapterInterface API before opening the mini program package for the first time. Otherwise, public key initialization will fail. For details about public and private keys, see Configure the mini program Package > Manage keys.
    • Enable signature verification.
           
      1. [MPNebulaAdapterInterface shareInstance].nebulaNeedVerify = YES;
  5. Delete the local Mini Program.
    Nebula provides the API for deleting local App information. After local App information is deleted, opening the App again requests the server to download and update local Mini Program information again.
       
    1. /**
    2. * @brief Delete local app information (including package information, amr, and installation directory)
    3. *
    4. * @date 2019-02-28
    5. *
    6. * @return
    7. */
    8. -(void)clearAllAppInfo:(NSString *)appId;
    9. // Method to use
    10. [[NBServiceGet() appCenter] clearAllAppInfo:@"2020199503242811"];