Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add possibility to manually check in foreground #414

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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