iOS原生和UniApp通信

1) 原生(OC)向 UniApp发送消息:

iOS项目中代码:

复制代码
 [self.uniMPInstance sendUniMPEvent:@"NativeEvent" data:@{@"msg":@"native message"}];

UniApp项目中代码:

复制代码
 uni.onNativeEventReceive((event,data)=>{
				console.log('接收到宿主App消息:' + event + data);
				this.nativeMsg = '接收到宿主App消息 event:' + event + " data: " + data;
			})
2) UniApp 向原生发送消息(打开原始页面):

UniApp代码:

复制代码
uni.sendNativeEvent('unimp-event', {
					msg: 'unimp message!!!'
				}, ret => {
					this.nativeMsg = '宿主App回传的数据:' + ret;
				})

iOS代码:

复制代码
/// 小程序向原生发送事件回调方法
/// @param appid 对应小程序的appid
/// @param event 事件名称
/// @param data 数据:NSString 或 NSDictionary 类型
/// @param callback 回调数据给小程序
- (void)onUniMPEventReceive:(NSString *)appid event:(NSString *)event data:(id)data callback:(DCUniMPKeepAliveCallback)callback {
    NSLog(@"Receive UniMP:%@ event:%@ data:%@",appid,event,data);
    // 根据event字段跳转不同页面
 /// 小程序打开状态,调用此方法可获取小程序对应的 DCUniMPViewController 实例
    UIViewController *uniMPVC = [DCUniMPSDKEngine getUniMPViewController];
    OriginViewController *vc = [[OriginViewController alloc] init];

//    vc.modalPresentationStyle = UIModalPresentationFullScreen;
//
//    [uniMPVC presentViewController:vc animated:YES completion:nil];
    // 前提是打开Uniapp的方式为push,即 configuration.openMode = DCUniMPOpenModePush;
    [uniMPVC.navigationController pushViewController:vc animated:YES];

     // 回传数据给小程序
    // DCUniMPKeepAliveCallback 用法请查看定义说明
    if (callback) {
        callback(@"native callback message",NO);
    }
    }
3) 原生 打开UniApp页面:

UniApp代码:

复制代码
// 获取配置信息
    DCUniMPConfiguration *configuration = [[DCUniMPConfiguration alloc] init];
    
    // 配置启动小程序时传递的数据(目标小程序可在 App.onLaunch,App.onShow 中获取到启动时传递的数据)
    configuration.extraData = @{ @"launchInfo":@"Hello UniMP" };
    
    // 配置小程序启动后直接打开的页面路径 例:@"pages/component/view/view?action=redirect&password=123456"
    configuration.path = @"pages/component/view/view?action=redirect&password=123456";
    
    // 开启后台运行
    configuration.enableBackground = YES;
    
    // 设置打开方式
    configuration.openMode = DCUniMPOpenModePush;
    
    // 启用侧滑手势关闭小程序
    configuration.enableGestureClose = YES;
    
    __weak __typeof(self)weakSelf = self;
    // 打开小程序
    [DCUniMPSDKEngine openUniMP:k_AppId1 configuration:configuration completed:^(DCUniMPInstance * _Nullable uniMPInstance, NSError * _Nullable error) {
        if (uniMPInstance) {
            weakSelf.uniMPInstance = uniMPInstance;
        } else {
            NSLog(@"打开小程序出错:%@",error);
        }
    }];
相关推荐
spmcor5 小时前
身份证读卡“无感登录”方案实践:从手动点击到自动检测
uni-app
PedroQue9911 小时前
uni-router v1.8.0新增冷启动守卫补执行
前端·uni-app
songgeb1 天前
启发式 UI 自动化:从线性剧本到每步读屏决策
ios·测试
PedroQue991 天前
uni-router v1.7.0重磅更新:守卫重定向自由掌控
前端·uni-app
一份执念3 天前
uni-app项目 (vue+vite + uni-UI)中引入umd格式JS文件,微信小程序中导入报错处理方案
前端·uni-app·echarts
PedroQue993 天前
V1.6.1性能优化:高频路径提速与代码精简
前端·uni-app
壹方秘境5 天前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios
夏碧笔5 天前
uni-app跨端地图实战:用第三方LBS替代微信平台收费服务
uni-app
用户69903048487510 天前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_10 天前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app