宿主应用启动小程序时可实现如下功能
- -
下面讲解一下具体实现
API 说明
iOS 平台
Class DCUniMPSDKEngine/// 启动小程序+ (void)openApp:(NSString *)appid arguments:(NSDictionary * _Nullable)arguments redirectPath:(NSString * _Nullable)redirectPath;
Android 平台
Tips uni小程序SDK API参考手册// 启动小程序DCUniMPSDK.getInstance().startApp(context, appid, splashClass, redirectPath, arguments)
启动小程序并传参
plus.runtime.arguments获取宿主传入的参数 iOS 示例
Android 示例// 启动小程序并传入参数NSDictionary *arguments = @{@"value":@"Hello uni microprogram"};[DCUniMPSDKEngine openApp:appid arguments:arguments];
小程序中获取参数// 启动小程序并传入参数 "Hello uni microprogram"try { JSONObject arguments = new JSONObject(); arguments.put("MSG","Hello uni microprogram"); DCUniMPSDK.getInstance().startApp(context, "__UNI__04E3A11", arguments);} catch (Exception e) { e.printStackTrace();}
var arguments = plus.runtime.arguments;
启动打开指定页面
宿主启动小程序时可通过传入页面路径来打开指定页面 页面路径格式要求 路径从 pages/ 开始填写绝对路径并支持参数 示例:
iOS 示例pages/component/view/view?action=redirect
Android 示例// 启动直达页面NSString *pagePath = @"pages/component/view/view?action=redirect";[DCUniMPSDKEngine openApp:k_AppId arguments:nil redirectPath:pagePaht];
屏蔽返回// 启动直达页面DCUniMPSDK.getInstance().startApp(context,"__UNI__04E3A11", "pages/component/view/view?action=redirect");
onLoad()方法中获取当前页面调用setStyle()方法实现,代码如下 TipsonLoad(e){}方法的参数e即为直达页面时传入的参数比如pages/component/view/view?action=redirect,框架会自动将参数转换成 jsonObject 类型 {“action”:”redirect”}// 启动直达二级页面屏蔽返回手势及返回按钮<script> export default { onLoad(e) { // #ifdef APP-PLUS // 判断是否为启动直达页面 if (e.action === "redirect") { const currentWebview = this.$scope.$getAppWebview(); currentWebview.setStyle({ popGesture: 'none', // 取消手势返回 titleNView: { autoBackButton: false // 取消默认返回按钮 } }) } // #endif } }</script>
在 GitHub 上编辑此页面!
