wendy_
wendy_
  • 发布:2024-03-06 17:54
  • 更新:2024-03-06 17:54
  • 阅读:143

iOS 端扩展插件方法中调用NSNotificationCenter,app release环境下出现闪退,debug 环境下正常,不会出现闪退

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Mac

PC开发环境操作系统版本号: 14.3.1

HBuilderX类型: 正式

HBuilderX版本号: 4.01

手机系统: iOS

手机系统版本号: iOS 11.0

手机厂商: 苹果

手机机型: iphone7

页面类型: vue

vue版本: vue2

打包方式: 离线

项目创建方式: HBuilderX

示例代码:

AppDelegate.m 文件

// iOS 10 Support

  • (void)jpushNotificationCenter:(UNUserNotificationCenter )center didReceiveNotificationResponse:(UNNotificationResponse )response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
    }

    // 在处理通知点击逻辑的地方, 发送广播事件
    [[NSNotificationCenter defaultCenter] postNotificationName:@"JiguangBroadcastNotification" object:nil userInfo:userInfo];
    completionHandler(); // 系统要求执行这个方法

    }

扩展插件文件CustomPushClickModule.m

// 通过宏 UNI_EXPORT_METHOD_SYNC 将同步方法暴露给 js 端
UNI_EXPORT_METHOD_SYNC(@selector(registerSyncClickHandler:))

/// 同步方法(注:同步方法会在 js 线程执行)
/// @param options js 端调用方法时传递的参数

  • (NSString )registerSyncClickHandler:(NSDictionary )options {
    // [[EventBus sharedInstance] subscribeToEventWithName:@"GlobalEvent" handler:^(NSDictionary userInfo) {
    // NSString
    message = userInfo[@"message"];
    // NSLog(@"ClassA Received data: %@", message);
    // }];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receiveBroadcastNotification:) name:@"JiguangBroadcastNotification" object:nil];

    // 同步返回参数给 js 端 注:只支持返回 String 或 NSDictionary (map) 类型
    return @"success";
    }

  • (void)receiveBroadcastNotification:(NSNotification *)notification {
    // 处理接收到的通知
    }

前端调用

var CustomPushClickModule = uni.requireNativePlugin("CustomPushClickModule")
CustomPushClickModule.registerSyncClickHandler({code:23})

操作步骤:

AppDelegate.m 文件

// iOS 10 Support

  • (void)jpushNotificationCenter:(UNUserNotificationCenter )center didReceiveNotificationResponse:(UNNotificationResponse )response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
    }

    // 在处理通知点击逻辑的地方, 发送广播事件
    [[NSNotificationCenter defaultCenter] postNotificationName:@"JiguangBroadcastNotification" object:nil userInfo:userInfo];
    completionHandler(); // 系统要求执行这个方法

    }

扩展插件文件CustomPushClickModule.m

// 通过宏 UNI_EXPORT_METHOD_SYNC 将同步方法暴露给 js 端
UNI_EXPORT_METHOD_SYNC(@selector(registerSyncClickHandler:))

/// 同步方法(注:同步方法会在 js 线程执行)
/// @param options js 端调用方法时传递的参数

  • (NSString )registerSyncClickHandler:(NSDictionary )options {
    // [[EventBus sharedInstance] subscribeToEventWithName:@"GlobalEvent" handler:^(NSDictionary userInfo) {
    // NSString
    message = userInfo[@"message"];
    // NSLog(@"ClassA Received data: %@", message);
    // }];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receiveBroadcastNotification:) name:@"JiguangBroadcastNotification" object:nil];

    // 同步返回参数给 js 端 注:只支持返回 String 或 NSDictionary (map) 类型
    return @"success";
    }

  • (void)receiveBroadcastNotification:(NSNotification *)notification {
    // 处理接收到的通知
    }

前端调用

onLaunch: function() {
var CustomPushClickModule = uni.requireNativePlugin("CustomPushClickModule")
CustomPushClickModule.registerSyncClickHandler({code:23})
}

预期结果:

期望release版本下不闪退

实际结果:

release版本下闪退

bug描述:


iOS 端扩展插件方法中调用NSNotificationCenter广播注册事件,app release环境下出现闪退报错com.taobao.weex.bridge (9): signal SIGABRT```,debug  环境下正常,不会出现闪退
2024-03-06 17:54 负责人:DCloud_App_Array 分享
已邀请:

要回复问题请先登录注册