-
-
Save MacKentoch/1b57759f3603c5ec42a8ce7c8b55e54e to your computer and use it in GitHub Desktop.
Example RCTEventEmitter Subclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RCTEventEmitter.h" | |
#import "RCTBridge.h" | |
@interface GSEventEmitter : RCTEventEmitter <RCTBridgeModule> | |
+ (BOOL)application:(UIApplication *)application didSightBeacon:(NSString *)beaconID; | |
+ (BOOL)application:(UIApplication *)application didDepartBeacon:(NSString *)beaconID; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GSEventEmitter.h" | |
// Notification/Event Names | |
NSString *const kBeaconSighted = @"GSEventEmitter/beaconSighted"; | |
NSString *const kBeaconDeparted = @"GSEventEmitter/beaconDeparted"; | |
@implementation GSEventEmitter | |
RCT_EXPORT_MODULE(); | |
- (NSDictionary<NSString *, NSString *> *)constantsToExport { | |
return @{ @"BEACON_SIGHTED": kBeaconSighted, | |
@"BEACON_DEPARTED": kBeaconDeparted, | |
}; | |
} | |
- (NSArray<NSString *> *)supportedEvents { | |
return @[kBeaconSighted, | |
kBeaconDeparted | |
]; | |
} | |
- (void)startObserving { | |
for (NSString *event in [self supportedEvents]) { | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(handleNotification:) | |
name:event | |
object:nil]; | |
} | |
} | |
- (void)stopObserving { | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
# pragma mark Public | |
+ (BOOL)application:(UIApplication *)application didSightBeacon:(NSString *)beaconID { | |
[self postNotificationName:kBeaconSighted withPayload:beaconID]; | |
return YES; | |
} | |
+ (BOOL)application:(UIApplication *)application didDepartBeacon:(NSString *)beaconID { | |
[self postNotificationName:kBeaconDeparted withPayload:beaconID]; | |
return YES; | |
} | |
# pragma mark Private | |
+ (void)postNotificationName:(NSString *)name withPayload:(NSObject *)object { | |
NSDictionary<NSString *, id> *payload = @{@"payload": object}; | |
[[NSNotificationCenter defaultCenter] postNotificationName:name | |
object:self | |
userInfo:payload]; | |
} | |
- (void)handleNotification:(NSNotification *)notification { | |
[self sendEventWithName:notification.name body:notification.userInfo]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment