美文网首页
iOS调RN方法,bridge爆错

iOS调RN方法,bridge爆错

作者: 我叫没名字啊 | 来源:发表于2017-10-20 16:48 被阅读90次

    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 <Foundation/Foundation.h>
    
    #import <React/RCTBridgeModule.h>
    #import <React/RCTEventEmitter.h>
    
    
    @interface RNBridgeTools : RCTEventEmitter <RCTBridgeModule>
    + (RNBridgeTools *)sharedInstance;
    
    //把推送消息体,推给RN
    - (void)callRNWithNotificationInfo:(NSDictionary *)info;
    
    //TouchId的结果回调
    - (void)touchIdVerity:(BOOL)success error:(NSError *)error;
    
    @end
    
    //  RNBridgeTools.m
    
    #import "RNBridgeTools.h"
    #import <React/RCTEventDispatcher.h>
    #import <React/RCTLog.h>
    
    @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<NSString *> *)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爆错

          本文链接:https://www.haomeiwen.com/subject/pqfnrxtx.html