ios native 调用 js 代码的时候 出问题了, 求助



  • @interface NativeManager : NSObject <RCTBridgeModule>
    @end

    @implementation NativeManager

    RCT_EXPORT_MODULE();

    @synthesize bridge = _bridge;

    if (self.bridge == nil) {
    NSLog(@"self. bridge is nil");
    } else {
    [self.bridge.eventDispatcher sendAppEventWithName:@"didRegisterToken"
    body: eventName];
    }

    这是我iOS发送 消息的代码, 但是 _bridge 属性 一直是为nil 的 ,我不知道是不是哪个步骤我少了,求大神帮忙


  • administrators

    好像是需要单例模式



  • @sunnylqm 其实我本来用的就是单例模式,,只是为了方便读 截取了部分代码
    下面是我的全部代码,,不过 bridge 就是为nil stackoverflow 上也有这个问题,不过还没有正确答案:sweat:

    /// .h
    @interface JPushHelper : NSObject <RCTBridgeModule>
    @property(strong,nonatomic)NSString *myname;
    + (JPushHelper *)shareInstance;
    - (void)didRegistRemoteNotification:(NSString *)token;
    @end
    
    
    
    //.m
    @implementation JPushHelper
    
    RCT_EXPORT_MODULE();
    @synthesize bridge = _bridge;
    
    + (JPushHelper *)shareInstance {
      static JPushHelper *jpushManager = nil;
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
        jpushManager = [[JPushHelper alloc] init];
      });
      return jpushManager;
    }
    
    - (dispatch_queue_t)methodQueue
    {
      return dispatch_get_main_queue();
    }
    
    - (void)didRegistRemoteNotification:(NSString *)token {
      if (self.bridge == nil) {
        NSLog(@" the bridge  is nil");
      }
      [self.bridge.eventDispatcher sendAppEventWithName:@"didRegisterToken"
                                                   body:token];
    }
    
    RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
    {
      NSLog(@" success ");
    }
    @end
    
    
    //这里是调用的代码
    - (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
      NSString *eventName = deviceToken.description;
      [[JPushHelper shareInstance] didRegistRemoteNotification:deviceToken.description];
    }
    


  • 我的问题找到了,,,

    应该用单例模式,,不过这个单例和平常写的不太一样,,泪崩啊

    直接贴代码把

     + (id)allocWithZone:(NSZone *)zone {
      static JPushHelper *sharedInstance = nil;
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
        sharedInstance = [super allocWithZone:zone];
      });
      return sharedInstance;
    }
    


  • 特意登录了一下,来回帖表示感谢,解决了我的一大问题,多谢多谢。


登录后回复