React-native Bridge is Nil when I call a method from another method

but I noticed that my bridge variable is zero when I call it from another method. I believe that this is due to the fact that the bridge is set only when the bridge method is called from javascript. I tried everything from creating a delegate to creating a SingleTon class. None of the above work, and I can’t understand why it is only available in the method that I called from Javascript. Here is my class

helper.h

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

@interface Helper : RCTEventEmitter <RCTBridgeModule>

-(void) auth;

@end

Here is my .m file: 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

I call this method from within my delegate as follows:

[[AppDelegate alloc] init] auth]

That is why I believe that it is not initialized. I'm not sure how to get the RCTBridge variable so that it is not null. Any help?

+4
2

:

[[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

: JS

+7

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"}];

, -!

+2

Source: https://habr.com/ru/post/1650480/


All Articles