Skip to content

Commit

Permalink
feat: add possibility to manually check in foreground (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhao900914 authored Oct 5, 2022
1 parent 9e3081a commit 9b8ada6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
56 changes: 31 additions & 25 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions Sources/Amplitude/Public/Amplitude.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions Tests/AmplitudeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

0 comments on commit 9b8ada6

Please sign in to comment.