diff --git a/CHANGELOG.md b/CHANGELOG.md index 53435fcc..a4a51605 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## 24.7.1 * Added `enableTemporaryDeviceIDMode` methods both at the Config level and after initialization to enable temporary device ID mode. +* Fixed an automatic start session issue where a session could be started while the app was in the background, resulting in 0 duration sessions being sent. + ## 24.7.0 * Implemented automatic sending of user properties to the server without requiring an explicit call to the `save` method diff --git a/Countly.m b/Countly.m index 1c5c40a8..def8eece 100644 --- a/Countly.m +++ b/Countly.m @@ -60,11 +60,25 @@ - (instancetype)init selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil]; + + [NSNotificationCenter.defaultCenter addObserver:self + selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification + object:nil]; + [NSNotificationCenter.defaultCenter addObserver:self + selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification + object:nil]; #elif (TARGET_OS_OSX) [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; + + [NSNotificationCenter.defaultCenter addObserver:self + selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification + object:nil]; + [NSNotificationCenter.defaultCenter addObserver:self + selector:@selector(applicationWillResignActive:) name:NSApplicationWillResignActiveNotification + object:nil]; #endif } @@ -404,6 +418,17 @@ - (void)resume isSuspended = NO; } +- (void)applicationDidBecomeActive:(NSNotification *)notification +{ + CLY_LOG_D(@"App enters foreground"); + [self resume]; +} + +- (void)applicationWillResignActive:(NSNotification *)notification +{ + CLY_LOG_D(@"App enters background"); +} + - (void)applicationDidEnterBackground:(NSNotification *)notification { CLY_LOG_D(@"App did enter background."); @@ -413,7 +438,6 @@ - (void)applicationDidEnterBackground:(NSNotification *)notification - (void)applicationWillEnterForeground:(NSNotification *)notification { CLY_LOG_D(@"App will enter foreground."); - [self resume]; } - (void)applicationWillTerminate:(NSNotification *)notification @@ -431,6 +455,7 @@ - (void)applicationWillTerminate:(NSNotification *)notification [CountlyPersistency.sharedInstance saveToFileSync]; } + - (void)dealloc { [NSNotificationCenter.defaultCenter removeObserver:self]; diff --git a/CountlyConnectionManager.m b/CountlyConnectionManager.m index f9cbb89c..5d80367c 100644 --- a/CountlyConnectionManager.m +++ b/CountlyConnectionManager.m @@ -354,6 +354,11 @@ - (void)beginSession CLY_LOG_W(@"%s A session is already running, this 'beginSession' will be ignored", __FUNCTION__); return; } + + if (!CountlyCommon.sharedInstance.manualSessionHandling && [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { + CLY_LOG_W(@"%s App is in the background, 'beginSession' will be ignored", __FUNCTION__); + return; + } isSessionStarted = YES; lastSessionStartTime = NSDate.date.timeIntervalSince1970;