Skip to content

Commit

Permalink
Merge pull request #1 from sharekris/swizzle_on_load
Browse files Browse the repository at this point in the history
Moved the code to observe for the UIApplicationDidFinishLaunchingNoti…
  • Loading branch information
TorreyKahuna committed Jul 31, 2015
2 parents d288ab9 + e6f6271 commit a1033ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
7 changes: 4 additions & 3 deletions Analytics/Integrations/Kahuna/SEGKahunaIntegration.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@


@interface SEGKahunaPushMonitor : NSObject
@property (nonatomic) NSDictionary *pushInfo;
@property (nonatomic) UIApplicationState applicationState;
@property (nonatomic) BOOL kahunaInitialized;
@property (atomic) NSDictionary *pushInfo;
@property (atomic) UIApplicationState applicationState;
@property (atomic) BOOL kahunaInitialized;
@property (atomic) NSError *failedToRegisterError;
@property Class kahunaClass;

+ (instancetype)sharedInstance;
Expand Down
44 changes: 29 additions & 15 deletions Analytics/Integrations/Kahuna/SEGKahunaIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ @implementation SEGKahunaIntegration
+ (void)load
{
[SEGAnalytics registerIntegration:self withIdentifier:@"Kahuna"];

// When this class loads we will register for the 'UIApplicationDidFinishLaunchingNotification' notification.
// To receive the notification we will use the SEGKahunaPushMonitor singleton instance.
[[NSNotificationCenter defaultCenter] addObserver:[SEGKahunaPushMonitor sharedInstance]
selector:@selector(didFinishLaunching:)
name:UIApplicationDidFinishLaunchingNotification
object:nil];
}

- (id)init
Expand Down Expand Up @@ -60,7 +67,14 @@ - (void)start
[self.kahunaClass performSelector:@selector(setSDKWrapper:withVersion:) withObject:SEGMENT withObject:[SEGAnalytics version]];
#pragma GCC diagnostic pop
[self.kahunaClass launchWithKey:apiKey];
// If we have recorded any push user info, then

// If we have a push token registration failure, then call the Kahuna handleNotificationRegistrationFailure method.
if ([SEGKahunaPushMonitor sharedInstance].failedToRegisterError != nil) {
[self.kahunaClass handleNotificationRegistrationFailure:[SEGKahunaPushMonitor sharedInstance].failedToRegisterError];
[SEGKahunaPushMonitor sharedInstance].failedToRegisterError = nil;
}

// If we have recorded any push info, then call the Kahuna handleNotification method.
if ([SEGKahunaPushMonitor sharedInstance].pushInfo != nil) {
[self.kahunaClass handleNotification:[SEGKahunaPushMonitor sharedInstance].pushInfo withApplicationState:[SEGKahunaPushMonitor sharedInstance].applicationState];
[SEGKahunaPushMonitor sharedInstance].pushInfo = nil;
Expand All @@ -70,13 +84,6 @@ - (void)start
}
}

// When this class loads we will register for the 'UIApplicationDidFinishLaunchingNotification' notification.
// To receive the notification we will use the SEGKahunaPushMonitor singleton instance.
[[NSNotificationCenter defaultCenter] addObserver:[SEGKahunaPushMonitor sharedInstance]
selector:@selector(didFinishLaunching:)
name:UIApplicationDidFinishLaunchingNotification
object:nil];

[super start];
}

Expand Down Expand Up @@ -399,10 +406,7 @@ - (void)swizzleAppDelegateMethods
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
@try {
if ([SEGKahunaPushMonitor sharedInstance].kahunaInitialized) {
[self.kahunaClass handleNotificationRegistrationFailure:error];
}

[[SEGKahunaPushMonitor sharedInstance] failedToRegisterPush:error];
if (selOriginalApplicationDidFailToRegisterForRemoteNotificationsWithError) {
selOriginalApplicationDidFailToRegisterForRemoteNotificationsWithError([UIApplication sharedApplication].delegate,
@selector(application:didFailToRegisterForRemoteNotificationsWithError:),
Expand All @@ -418,7 +422,7 @@ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotif
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
@try {
[self pushReceived:userInfo];
[[SEGKahunaPushMonitor sharedInstance] pushReceived:userInfo];
if (selOriginalApplicationDidReceiveRemoteNotification) {
selOriginalApplicationDidReceiveRemoteNotification([UIApplication sharedApplication].delegate,
@selector(application:didReceiveRemoteNotification:),
Expand All @@ -434,7 +438,7 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
@try {
[self pushReceived:userInfo];
[[SEGKahunaPushMonitor sharedInstance] pushReceived:userInfo];
if (selOriginalApplicationDidReceiveRemoteNotificationWithFetchCompletionHandler) {
selOriginalApplicationDidReceiveRemoteNotificationWithFetchCompletionHandler([UIApplication sharedApplication].delegate,
@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:),
Expand All @@ -451,7 +455,7 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
@try {
[self pushReceived:userInfo];
[[SEGKahunaPushMonitor sharedInstance] pushReceived:userInfo];
if (selOriginalApplicationHandleActionWithIdentifierWithFetchCompletionHandler) {
selOriginalApplicationHandleActionWithIdentifierWithFetchCompletionHandler([UIApplication sharedApplication].delegate,
@selector(application:handleActionWithIdentifier:forRemoteNotification:completionHandler:),
Expand Down Expand Up @@ -481,4 +485,14 @@ - (void)pushReceived:(NSDictionary *)userInfo
}
}

- (void)failedToRegisterPush:(NSError *)error
{
// When we get this failure, check if kahuna is initialized. If not store it for future use.
if ([SEGKahunaPushMonitor sharedInstance].kahunaInitialized) {
[self.kahunaClass handleNotificationRegistrationFailure:error];
} else {
[SEGKahunaPushMonitor sharedInstance].failedToRegisterError = error;
}
}

@end

0 comments on commit a1033ca

Please sign in to comment.