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

Preventing SDK to automatics start session in background #320

Merged
merged 4 commits into from
Jul 24, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 26 additions & 1 deletion Countly.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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.");
Expand All @@ -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
Expand All @@ -431,6 +455,7 @@ - (void)applicationWillTerminate:(NSNotification *)notification
[CountlyPersistency.sharedInstance saveToFileSync];
}


- (void)dealloc
{
[NSNotificationCenter.defaultCenter removeObserver:self];
Expand Down
5 changes: 5 additions & 0 deletions CountlyConnectionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mb we can also add this check to the updateSession

CLY_LOG_W(@"%s App is in the background, 'beginSession' will be ignored", __FUNCTION__);
return;
}

isSessionStarted = YES;
lastSessionStartTime = NSDate.date.timeIntervalSince1970;
Expand Down