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);
        }
    }];
相关推荐
疯华整猫3 小时前
别再抄 Flutter 了:2026 年 Android / iOS / 小程序跨端架构选型的四本成本账
android·前端·ios
黑科技iOS上架3 小时前
iOS游戏换皮上架困境:4.3同质化拒审
人工智能·ios·审核
黑科技iOS上架3 小时前
iOS二次开发应用上架难题:4.3a同质化拒审
ios·审核
传奇开心果编程3 小时前
【Flutter踩坑排雷】第30课:异常未捕获坑|未捕获异常直接闪退,全局兜底故障处理
android·flutter·ios·移动开发·dart·ui框架·声明式ui
茶底世界之下4 小时前
海边游乐场的黄昏:用 Awaking 留住一层暖光
ios·app·产品
90后的晨仔16 小时前
Xcode 27.1 下,老工程如何在 iPhone Duo 模拟器上跑起来
ios
90后的晨仔17 小时前
Xcode 27 升级后满屏 deployment target 报错?一文搞定
ios
二流小码农20 小时前
鸿蒙开发:router组件升级,彻底告别手动配置
android·ios·harmonyos
茶底世界之下1 天前
雾中山路的冷光层次:用 Awaking 留住红色背影
ios·app·产品
Digitally1 天前
iPhone 如何添加日历?
ios·iphone