From afd7518e2155397d65bff698102dbc6a9eec455c Mon Sep 17 00:00:00 2001 From: yuhao900914 Date: Wed, 5 Oct 2022 12:42:42 -0700 Subject: [PATCH] feat: add possibility to manually check in foreground --- Sources/Amplitude/Amplitude.m | 56 +++++++++++++++------------- Sources/Amplitude/Public/Amplitude.h | 10 +++++ Tests/AmplitudeTests.m | 19 ++++++++++ 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/Sources/Amplitude/Amplitude.m b/Sources/Amplitude/Amplitude.m index aa41488a..c5321590 100644 --- a/Sources/Amplitude/Amplitude.m +++ b/Sources/Amplitude/Amplitude.m @@ -465,33 +465,39 @@ - (void)initializeApiKey:(NSString *)apiKey } }]; - // Normally _inForeground is set by the enterForeground callback, but initializeWithApiKey will be called after the app's enterForeground - // notification is already triggered, so we need to manually check and set it now. - // UIApplication methods are only allowed on the main thread so need to dispatch this synchronously to the main thread. - void (^checkInForeground)(void) = ^{ - #if !TARGET_OS_OSX && !TARGET_OS_WATCH - UIApplication *app = [AMPUtils getSharedApplication]; - if (app != nil) { - UIApplicationState state = app.applicationState; - if (state != UIApplicationStateBackground) { - [self runOnBackgroundQueue:^{ - #endif - // The earliest time to fetch dynamic config - [self refreshDynamicConfig]; - - NSNumber *now = [NSNumber numberWithLongLong:[[self currentTime] timeIntervalSince1970] * 1000]; - [self startOrContinueSessionNSNumber:now]; - self->_inForeground = YES; - #if !TARGET_OS_OSX && !TARGET_OS_WATCH - }]; + if (!self.deferCheckInForeground) { + [self checkInForeground]; + } + } +} + +- (void) checkInForeground { + // Normally _inForeground is set by the enterForeground callback, but initializeWithApiKey will be called after the app's enterForeground + // notification is already triggered, so we need to manually check and set it now. + // UIApplication methods are only allowed on the main thread so need to dispatch this synchronously to the main thread. + void (^checkInForeground)(void) = ^{ + #if !TARGET_OS_OSX && !TARGET_OS_WATCH + UIApplication *app = [AMPUtils getSharedApplication]; + if (app != nil) { + UIApplicationState state = app.applicationState; + if (state != UIApplicationStateBackground) { + [self runOnBackgroundQueue:^{ + #endif + // The earliest time to fetch dynamic config + [self refreshDynamicConfig]; + + NSNumber *now = [NSNumber numberWithLongLong:[[self currentTime] timeIntervalSince1970] * 1000]; + [self startOrContinueSessionNSNumber:now]; + self->_inForeground = YES; + #if !TARGET_OS_OSX && !TARGET_OS_WATCH + }]; - } } - #endif - }; - [self runSynchronouslyOnMainQueue:checkInForeground]; - _initialized = YES; - } + } + #endif + }; + [self runSynchronouslyOnMainQueue:checkInForeground]; + _initialized = YES; } /** diff --git a/Sources/Amplitude/Public/Amplitude.h b/Sources/Amplitude/Public/Amplitude.h index 97adde50..38ae5e71 100644 --- a/Sources/Amplitude/Public/Amplitude.h +++ b/Sources/Amplitude/Public/Amplitude.h @@ -196,6 +196,12 @@ typedef void (^AMPInitCompletionBlock)(void); */ @property (nonatomic, strong, nullable) AMPInitCompletionBlock initCompletionBlock; +/** + * Defer the forground check in initializeApiKey. + * checkInForeground need to be manually called in order to get the right config and session info if deferCheckInForeground = true has been set. + */ +@property (nonatomic, assign) BOOL deferCheckInForeground; + #pragma mark - Methods /**----------------------------------------------------------------------------- @@ -251,6 +257,10 @@ typedef void (^AMPInitCompletionBlock)(void); */ - (void)initializeApiKey:(NSString *)apiKey userId:(nullable NSString *)userId; +/** +* Manually check in and set the forground related settings including dynamic config and sesstion. Need to be called manually when onEnterForeground if deferCheckInForeground = true. +*/ +- (void)checkInForeground; /**----------------------------------------------------------------------------- * @name Logging Events diff --git a/Tests/AmplitudeTests.m b/Tests/AmplitudeTests.m index 82f0cd76..ab2013c4 100644 --- a/Tests/AmplitudeTests.m +++ b/Tests/AmplitudeTests.m @@ -21,6 +21,8 @@ @interface Amplitude (Tests) +@property (nonatomic, assign) bool initialized; + - (NSDictionary*)mergeEventsAndIdentifys:(NSMutableArray*)events identifys:(NSMutableArray*)identifys numEvents:(long) numEvents; - (id)truncate:(id) obj; - (long long)getNextSequenceNumber; @@ -1199,5 +1201,22 @@ -(void)testLogEventWithUserProperties { XCTAssertEqualObjects([identifys[0] objectForKey:@"user_properties"], userProperties); } +-(void)testNoDeferCheckInForeground { + NSString *instanceName = @"noDeferCheckInForegroundInstance"; + Amplitude *client = [Amplitude instanceWithName:instanceName]; + [client initializeApiKey:@"api-key"]; + XCTAssertEqual(client.initialized, YES); +} + +-(void)testDeferCheckInForeground { + NSString *instanceName = @"DeferCheckInForegroundInstance"; + Amplitude *client = [Amplitude instanceWithName:instanceName]; + [client setDeferCheckInForeground:YES]; + [client initializeApiKey:@"api-key"]; + XCTAssertEqual(client.initialized, NO); + + [client checkInForeground]; + XCTAssertEqual(client.initialized, YES); +} @end