Skip to content

Instantly share code, notes, and snippets.

@andybangs
Created August 27, 2016 16:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybangs/5f1ee78247b0346e24d55119c6a02afa to your computer and use it in GitHub Desktop.
Save andybangs/5f1ee78247b0346e24d55119c6a02afa to your computer and use it in GitHub Desktop.
Example using GSEventEmitter
#import <Foundation/Foundation.h>
#import "RCTBridge.h"
@interface GSBeaconManager : NSObject <RCTBridgeModule>
@end
#import "GSBeaconManager.h"
#import "GSEventEmitter.h"
#import <Gimbal/Gimbal.h>
@interface GSBeaconManager () <GMBLBeaconManagerDelegate>
@property (nonatomic, strong) GMBLBeaconManager *beaconManager;
@end
@implementation GSBeaconManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(startListening){
[[self beaconManager] startListening];
}
- (instancetype)init {
self = [super init];
if (self) {
[Gimbal setAPIKey:kGMBLKey options:nil];
_beaconManager = [GMBLBeaconManager new];
_beaconManager.delegate = self;
}
return self;
}
- (void)beaconManager:(GMBLBeaconManager *)manager didReceiveBeaconSighting:(GMBLBeaconSighting *)sighting {
NSString *beaconID = sighting.beacon.identifier;
[GSEventEmitter application:[UIApplication sharedApplication] beaconSighted:beaconID];
}
@end
import React, { Component, PropTypes } from 'react';
import { NativeModules, NativeEventEmitter } from 'react-native';
class Welcome extends Component {
constructor(props) {
super(props);
this.eventEmitter;
}
componentWillMount() {
const { GSBeaconManager, GSEventEmitter } = NativeModules;
const { addBeacon } = this.props.actions;
this.eventEmitter = new NativeEventEmitter(GSEventEmitter);
this.eventEmitter.addListener(GSEventEmitter.SIGHTED_BEACON, (data) => addBeacon(data.payload));
GSBeaconManager.startListening();
}
componentWillUnmount() {
this.eventEmitter.remove();
}
...
}
@Symous
Copy link

Symous commented Aug 9, 2017

hi, friend. what confused me is that whether it is necessary for us to invoke startObserving method manually?

and another question , I can not understand this method, what are you trying to do with this ?

RCT_EXPORT_METHOD(startListening){
  [[self beaconManager] startListening];
}

I'm having difficulties to sending event from iOS native to js with RCTEventEmitter, thanks for your solution anyway!
: )

@mairi17
Copy link

mairi17 commented Jul 19, 2018

Thanks! This was really helpful for me 🥇

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