iOS调RN方法,bridge爆错

Q:

今天调试极光推送,需要iOS主动调用RN的方法,sendAppEvent,参考博客:http://blog.csdn.net/horisea/article/details/54176417

但是,sendAppEvent这个方法,在0.43之后就不建议使用了,可以用sendEvent,至于出现bridge找不到的情况,大家可以自行参考该文:https://github.com/facebook/react-native/issues/8714

A:

so,我目前的解决方法如下,如果有更好的办法, 欢迎大佬告知(手动比心❤️):

//  RNBridgeTools.h

#import 

#import 
#import 


@interface RNBridgeTools : RCTEventEmitter 
+ (RNBridgeTools *)sharedInstance;

//把推送消息体,推给RN
- (void)callRNWithNotificationInfo:(NSDictionary *)info;

//TouchId的结果回调
- (void)touchIdVerity:(BOOL)success error:(NSError *)error;

@end
//  RNBridgeTools.m

#import "RNBridgeTools.h"
#import 
#import 

@implementation RNBridgeTools
RCT_EXPORT_MODULE();

+ (RNBridgeTools *)sharedInstance
{
  __strong static RNBridgeTools * tools = nil;
  if (tools) {
    return tools;
  }
  
  static dispatch_once_t d;  
  dispatch_once(&d, ^{
    tools = [[RNBridgeTools alloc] init];
  });
  
  return tools;
}

- (NSArray *)supportedEvents
{
  return @[@"iOSRemoteNotification",@"iOSTouchIdVerity"];
}

#pragma mark - 把推送信息,推给RN
- (void)callRNWithNotificationInfo:(NSDictionary *)info
{
  [self.bridge.eventDispatcher sendAppEventWithName:@"iOSRemoteNotification" body:info];
}

#pragma mark - TouchId的结果回调
- (void)touchIdVerity:(BOOL)success error:(NSError *)error
{
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  [dict setObject:[NSNumber numberWithBool:success] forKey:@"success"];
  [dict setObject:[NSNumber numberWithInteger:error.code] forKey:@"errorCode"];
  [dict setObject:error.localizedDescription forKey:@"errorInfo"];
  
  [self.bridge.eventDispatcher sendAppEventWithName:@"iOSTouchIdVerity" body:dict];
}

@end

PS: 极光推送提供了 ReactNative 支持包,想用的同学可以参考:https://github.com/jpush/jpush-react-native

你可能感兴趣的:(iOS调RN方法,bridge爆错)