Jyotiska
Jyotiska

Reputation: 255

Displaying the time duration the app was in background

I am new to iOS programming, and I am trying to make a very simple app to know more about the lifecycle and different states. The app will store the current timestamp when the app is running, and whenever it goes background and comes back up, it will show a message like "Good to see you after xx minutes".

Now, my approach is something like this, at the beginning there will be a timestamp stored in a variable, and when the viewDidLoad() method is called, it will check the timestamp with the current timestamp and display the subtracted value.

When the app is going background, I will change the value of the local timestamp variable in viewDidUnload() method and when is coming back up I will compare the timestamps and display the message.

Any pointers regarding the correct approach of doing this?

Upvotes: 0

Views: 367

Answers (4)

Satheesh
Satheesh

Reputation: 11276

You can do something like this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterForeGround) name:UIApplicationDidEnterForegroundNotification object:nil];

-(void)didEnterBackground
{
 NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
 [userDefaults setObject:[[NSDate alloc]init] forKey:@"TimeStamp"];
 [userDefaults synchronize];
}

-(void)didEnterForeGround
{
 NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
 NSString *countString= [userDefaults objectForKey:@"TimeStamp"];  
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setTimeStyle:NSDateFormatterFullStyle];
 NSTimeInterval *timeBG=[[dateFormatter dateFromString:countString] timeIntervalSinceDate:[NSDate alloc] init]];
NSLog(@"Time Background: %@",timeBG);
}

Upvotes: 0

Pratik
Pratik

Reputation: 2399

use below code in your appdelegate.m file

put below method

-(void)minCalculation_backgroundtime:(NSString *)backgroundTime forgroundTime:(NSString *)foreGroundTime
{
    NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
    [dateformat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];

    NSDate *lastDate = [dateformat dateFromString:foreGroundTime];
    NSDate *todaysDate = [dateformat dateFromString:backgroundTime];
    NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
    NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
    NSTimeInterval dateDiff = lastDiff - todaysDiff;
    int min = dateDiff/60;
    NSLog(@"Good to see you after %i minutes",min);
}

and replace below two methods

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
    NSString *backGroundTime = [dateFormat stringFromDate:[NSDate date]];
    [[NSUserDefaults standardUserDefaults]setValue:backGroundTime forKey:@"backGroundTime"];
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
    NSString *foreGroundTime = [dateFormat stringFromDate:[NSDate date]];
    NSString *backGroundTime = [[NSUserDefaults standardUserDefaults]valueForKey:@"backGroundTime"];
    [self minCalculation_backgroundtime:backGroundTime forgroundTime:foreGroundTime];
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

try this your problem will solve

Upvotes: 1

Balu
Balu

Reputation: 8460

create one Global variable and pass the data between viewControllers and keep your code in below methods..

- (void)applicationDidEnterBackground:(UIApplication *)application 
- (void)applicationWillEnterForeground:(UIApplication *)application 

Upvotes: 0

Manish Agrawal
Manish Agrawal

Reputation: 11037

You can use NSUserDefaults for saving last timestamp.

Use this function when your app is going in background

- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillTerminate:(UIApplication *)application

when app again open take out the difference of last saved timestamp with current timestamp.

Upvotes: 0

Related Questions