当我从另一个方法调用方法时,React-native Bridge为Nil

时间:2016-08-07 20:33:45

标签: javascript ios objective-c react-native

但是我注意到每当我从另一个方法调用它时,我的桥变量都是nil。我相信这是因为仅在从javascript调用桥接方法时才设置桥。我已经尝试了从创建委托到创建SingleTon类的所有内容。上述工作都没有,我无法弄清楚为什么它仅在我从Javascript调用的方法中可用。这是我的班级

Helper.h

#import "RCTBridge.h"
#import "AppDelegate.h"
#import "RCTEventEmitter.h"

@interface Helper : RCTEventEmitter <RCTBridgeModule>

-(void) auth;

@end

这是我的.m文件: Helper.m

#import "AppDelegate.h"
#import "Helper.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"

@implementation Helper
RCT_EXPORT_MODULE();

@synthesize bridge = _bridge;

- (NSArray<NSString *> *)supportedEvents {
  return @[@"SpotifyHelper"];
}


RCT_EXPORT_METHOD(auth)
{
  [self.bridge.eventDispatcher sendDeviceEventWithName:@"SpotifyHelper" body:@{@"Login": @true}];
  printf("Auth");
}

RCT_EXPORT_METHOD(play:(NSString *) uri first: id)
{
  AppDelegate *appDelegate = [[AppDelegate alloc] init];
  [appDelegate play:uri second:id];
}

@end

我从我的委托内部调用该方法,如下所示:

[[AppDelegate alloc] init] auth]

我相信它没有初始化的原因。我不确定如何让RCTBridge变量不为零。有什么帮助吗?

2 个答案:

答案 0 :(得分:8)

问题在于:

[[AppDelegate alloc] init] auth]

当您使用宏RCT_EXPORT_MODULE()时,React-Native将为您实例化该类,并且任何后续alloc/init将创建与原始实例无关的新实例。该桥将不会在这些新实例中实例化。

您可以使用NSNotifications解决问题。

Helper.h:

#import "RCTEventEmitter.h"

@interface Helper : RCTEventEmitter

+ (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload;

@end

Helper.m:

#import "Helper.h"

@implementation Helper

RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents {
  return @[@"SpotifyHelper"];
}

- (void)startObserving
{
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(emitEventInternal:)
                                               name:@"event-emitted"
                                             object:nil];
}

- (void)stopObserving
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)emitEventInternal:(NSNotification *)notification
{
  [self sendEventWithName:@"SpotifyHelper"
                     body:notification.userInfo];
}

+ (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload
{
  [[NSNotificationCenter defaultCenter] postNotificationName:@"event-emitted"
                                                      object:self
                                                    userInfo:payload];
}

// Remaining methods

@end

这里有一个很长的讨论主题:What is the method can be used to send an event from native module to JS

答案 1 :(得分:3)

只是为了添加到oar.garuna的答案(这只是一个完整的生命保护程序),对于那些有多个事件且想要处理它的人,下面的代码将设置一个观察者,然后我们可以创建一个静态的调用emitEventWithName并且帮助者将适当地处理它并将该事件发送出去。

eventHelper.h

@interface eventHelper : RCTEventEmitter

  + (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload;

@end

eventHelper.m

@implementation eventHelper

  RCT_EXPORT_MODULE();

  // The list of available events
  - (NSArray<NSString *> *)supportedEvents {
    return @[@"EventLoggedOut"];
  }

  // This function listens for the events we want to send out and will then pass the
  // payload over to the emitEventInternal function for sending to Javascript
  - (void)startObserving
  {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(emitEventInternal:)
                                             name:@"event-emitted"
                                           object:nil];
  }

  // This will stop listening if we require it
  - (void)stopObserving
  {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
  }

  // This will actually throw the event out to our Javascript
  - (void)emitEventInternal:(NSNotification *)notification
  {
    // We will receive the dictionary here - we now need to extract the name
    // and payload and throw the event
NSArray *eventDetails = [notification.userInfo valueForKey:@"detail"];
    NSString *eventName = [eventDetails objectAtIndex:0];
    NSDictionary *eventData = [eventDetails objectAtIndex:1];

    [self sendEventWithName:eventName
                   body:eventData];
  }

  // This is our static function that we call from our code
  + (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload
  {
    // userInfo requires a dictionary so we wrap out name and payload into an array and stick
    // that into the dictionary with a key of 'detail'
    NSDictionary *eventDetail = @{@"detail":@[name,payload]};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"event-emitted"
                                                    object:self
                                                  userInfo:eventDetail];
  }

@end

一旦设置了该部分,您就可以从以下任何地方调用我们的静态函数emitEventWithName

[eventHelper emitEventWithName:@"myEventName" andPayload:@{@"key":@"value1"}];

希望能帮助别人!