I've made simple native ios module for game center authentication which works well until I reload my app cmd + r in ios simulator. After I refresh my app and this event emitter is called my app crashes with following error.
Bridge is not set. This is probably because you've explicitly synthesized the bridge in ASQGameCenter, even though it's inherited from RCTEventEmitter.'
Here is the module
#import "ASQGameCenter.h"
@implementation ASQGameCenter
/**
* Globals
*/
NSString *eventName = @"ASQGameCenter:event";
BOOL isSubscribed = NO;
/**
* Utalise main queue
*/
- (dispatch_queue_t)methodQueue {
return dispatch_get_main_queue();
}
RCT_EXPORT_MODULE();
/**
* Declare events
*/
- (NSArray<NSString *> *)supportedEvents {
return @[ eventName ];
}
/**
* Authenticate user
*/
RCT_EXPORT_METHOD(authenticate
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject) {
if (!isSubscribed) {
isSubscribed = YES;
[self subscribe];
}
resolve(@"ASQGameCenter:subscribe");
}
/**
* Subscribe to auth changes
*/
- (void)subscribe {
__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
UIViewController *rnView = [UIApplication sharedApplication].keyWindow.rootViewController;
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
if (error) {
[self sendEventWithName:eventName body:@{@"error" : error.description}];
}
else if (viewController != nil) {
[rnView presentViewController:viewController animated:YES completion:nil];
} else if (localPlayer.isAuthenticated) {
[self sendEventWithName:eventName body:@{@"playerID" : localPlayer.playerID}];
} else {
[self sendEventWithName:eventName body:@{@"error" : error.description}];
}
};
}
@end
And associated header file
#import <React/RCTEventEmitter.h>
#import "GameKit/GameKit.h"
@interface ASQGameCenter : RCTEventEmitter
@end