传统的小程序技术容易受到网络环境影响,当网络质量不佳时可能拉取不到小程序包。通过预置小程序即可规避该问题。本文介绍了预置小程序的原理和预置小程序的实现过程。
预置小程序是指将小程序的渲染、逻辑、配置等静态资源打包在一个压缩包内,客户端预先下载小程序包到本地,并直接从本地加载资源的过程。预置小程序可以最大程度地摆脱网络环境对 mPaaS 小程序页面的影响。使用预置包可带来以下优势:
您已接入小程序组件。更多关于小程序组件的接入信息,请参见 快速开始使用小程序。
DemoCustomPresetApps.bundle
,将从发布平台下载的 AMR 离线包和 h5_json.json
文件添加到此 bundle 中。h5_json.json
配置文件。当预置多个小程序包时,需要将不同 h5_json.json
中的 data 数据手动合并到一个配置文件中。initNebulaWithCustomPresetApplistPath
接口中设置预置小程序离线包路径为上一步中创建的 bundle。
- (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 初始化 rpc
[MPRpcInterface initRpc];
// 初始化容器
// [MPNebulaAdapterInterface initNebula];
// 自定义jsapi路径和预置小程序包信息
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];
}
[MPNebulaAdapterInterface startTinyAppWithId:@"2020121720201217" params:nil];
-(void)application:(UIApplication *)application afterDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//全量更新本地小程序包信息
[[MPNebulaAdapterInterface shareInstance] requestAllNebulaApps:^(NSDictionary *data, NSError *error) {
NSLog(@"[mpaas] nebula rpc data :%@", data);
}];
}
MPNebulaAdapterInterface
接口,否则将会导致公钥初始化失败。关于公钥与私钥,请参见 配置小程序包 > 密钥管理。
[MPNebulaAdapterInterface shareInstance].nebulaNeedVerify = YES;
/**
* @brief 删除本地应用信息(包括包信息、amr以及安装目录)
*
* @date 2019-02-28
*
* @return
*/
-(void)clearAllAppInfo:(NSString *)appId;
//使用方法
[[NBServiceGet() appCenter] clearAllAppInfo:@"2020199503242811"];
在文档使用中是否遇到以下问题
更多建议
匿名提交