Vkharb
Vkharb

Reputation: 886

Time for which application has stayed in background

I have an requirement for my app that after a certain period(25 mins) it has be logged out). Now how to calculate the time in which the application has stayed in a background?

i am trying to use the method -applicationDidBecomeActive and there is default code given for this using a time stamp and getting the difference between timestamps. the problem is that this difference is kind of coming odd and i am not able to understand it. Simply put i am to get the difference but i am not able convert into desired minutes/seconds. I would like to match if this difference is equal to 25 minutes or not(or even seconds would do) . Any simple code snippet?

Upvotes: 0

Views: 33

Answers (1)

rckoenes
rckoenes

Reputation: 69499

You set a NSDate where you enter de background:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    self.backgroundedDate = [NSDate date];
}

Then when you come back to the foreground check the time difference:

- (void)applicationWillEnterForeground:(UIApplication *)application {       
    if (self.backgroundedDate) {
        BOOL isTimedout =[self.backgroundedDate timeIntervalSinceNow] <= -(25 * 60);
        if (isTimedout) {
            [self.rootViewController presentLogin];
        }
    }
}

Upvotes: 3

Related Questions