|
| 1 | +#import <UIKit/UIKit.h> |
| 2 | +#import <objc/runtime.h> |
| 3 | + |
| 4 | +@interface RCTOneSignal |
| 5 | ++ (RCTOneSignal *) sharedInstance; |
| 6 | +- (void)initOneSignal; |
| 7 | +@end |
| 8 | + |
| 9 | +@implementation UIApplication(OneSignalReactNative) |
| 10 | + |
| 11 | +/* |
| 12 | + This UIApplication category ensures that OneSignal init() gets called at least one time |
| 13 | + |
| 14 | + If this did not occur, cold-start notifications would not trigger the React-Native 'opened' |
| 15 | + event and other miscellaneous problems would occur. |
| 16 | + |
| 17 | + First, we swizzle UIApplication's setDelegate method, to get notified when the app delegate |
| 18 | + is assigned. Then we swizzle UIApplication's didFinishLaunchingWithOptions() method. When |
| 19 | + this method gets called, it initializes the OneSignal SDK with a nil app ID. |
| 20 | +*/ |
| 21 | + |
| 22 | +//helper method to swizzle instance methods |
| 23 | +static void injectSelector(Class newClass, SEL newSel, Class addToClass, SEL makeLikeSel) { |
| 24 | + Method newMeth = class_getInstanceMethod(newClass, newSel); |
| 25 | + IMP imp = method_getImplementation(newMeth); |
| 26 | + const char* methodTypeEncoding = method_getTypeEncoding(newMeth); |
| 27 | + |
| 28 | + BOOL successful = class_addMethod(addToClass, makeLikeSel, imp, methodTypeEncoding); |
| 29 | + if (!successful) { |
| 30 | + class_addMethod(addToClass, newSel, imp, methodTypeEncoding); |
| 31 | + newMeth = class_getInstanceMethod(addToClass, newSel); |
| 32 | + |
| 33 | + Method orgMeth = class_getInstanceMethod(addToClass, makeLikeSel); |
| 34 | + |
| 35 | + method_exchangeImplementations(orgMeth, newMeth); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +//gets called by the ObjC runtime early in the app lifecycle |
| 40 | ++ (void)load { |
| 41 | + static dispatch_once_t onceToken; |
| 42 | + dispatch_once(&onceToken, ^{ |
| 43 | + method_exchangeImplementations(class_getInstanceMethod(self, @selector(setDelegate:)), class_getInstanceMethod(self, @selector(setOneSignalReactNativeDelegate:))); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +- (void) setOneSignalReactNativeDelegate:(id<UIApplicationDelegate>)delegate { |
| 49 | + static dispatch_once_t onceToken; |
| 50 | + dispatch_once(&onceToken, ^{ |
| 51 | + Class delegateClass = [delegate class]; |
| 52 | + |
| 53 | + injectSelector(self.class, @selector(oneSignalApplication:didFinishLaunchingWithOptions:), |
| 54 | + delegateClass, @selector(application:didFinishLaunchingWithOptions:)); |
| 55 | + [self setOneSignalReactNativeDelegate:delegate]; |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +- (BOOL)oneSignalApplication:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { |
| 60 | + [RCTOneSignal.sharedInstance initOneSignal]; |
| 61 | + |
| 62 | + if ([self respondsToSelector:@selector(oneSignalApplication:didFinishLaunchingWithOptions:)]) |
| 63 | + return [self oneSignalApplication:application didFinishLaunchingWithOptions:launchOptions]; |
| 64 | + return YES; |
| 65 | +} |
| 66 | + |
| 67 | +@end |
0 commit comments