Skip to content

Instantly share code, notes, and snippets.

@dratwas
Created November 20, 2017 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dratwas/92982973e974aaf066fedcc7406a5ac0 to your computer and use it in GitHub Desktop.
Save dratwas/92982973e974aaf066fedcc7406a5ac0 to your computer and use it in GitHub Desktop.
[self sendEventWithName:@"onSessionConnect" body:@{@"sessionId": session.sessionId}];
@PerspectivesLab
Copy link

from where do you call this function? its inaccessible from other implementation or classes...

@skantus
Copy link

skantus commented Nov 24, 2018

@PerspectivesLab https://facebook.github.io/react-native/docs/native-modules-ios#sending-events-to-javascript

RCT_EXPORT_METHOD(addEvent:(NSString *)name params:(NSDictionary *)params) {
  NSString *site = [RCTConvert NSString:params[@"site"]];
  [self notificationEvent];
  RCTLogInfo(@"Pretending to create an event %@ at %@", name, site);
}

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

// Will be called when this module's first listener is added.
-(void)startObserving {
  hasListeners = YES;
}

// Will be called when this module's last listener is removed, or on dealloc.
-(void)stopObserving {
  hasListeners = NO;
}

- (void) notificationEvent {
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sendEvent:) name:@"Eezy" object:@"my object"];
  [[NSNotificationCenter defaultCenter]postNotificationName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}];
}

- (void)sendEvent:(NSNotification *)notification {
  if (hasListeners) {
    RCTLogInfo(@"Event recieved.");
    [self sendEventWithName:@"onSessionConnect" body:@{@"body": notification.name}];
  } else {
    RCTLogInfo(@"Event stoped.");
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment