I added this code which functions as an auto timeout in my app. The userDefaults doubleForKey:@"timeoutLength" should be in minutes. For example, if value is 500, that should mean 500 minutes.
I keep seeming to be hitting the timout loop though even when it hasn’t really been 500 + min. Is anything wrong in my code? Perhaps a minutes/seconds error etc.
[userDefaults setDouble:[[userContextDictionary valueForKey:@"autologout_idle_timeout"] doubleValue] forKey:@"timeoutLength"];
double timeDifference = ([[NSDate date] timeIntervalSince1970] - [userDefaults doubleForKey:@"Close Time"]) / 60;
if (timeDifference > [userDefaults doubleForKey:@"timeoutLength"]) {
NSLog(@"Timeout Hit");
} else {
NSLog(@"No Timeout");
}
Edit:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[userDefaults setObject:[NSDate date] forKey:@"Close Time"];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[userDefaults setDouble:[[userContextDictionary valueForKey:@"autologout_idle_timeout"] doubleValue] forKey:@"timeoutLength"];
//This is an int like 500, or 600, etc.
NSDate *closeDate = [userDefaults objectForKey:@"Close Time"]
NSTimeInterval timeWhenClosedTimeInterval = [closeDate timeIntervalSince1970];
NSTimeInterval todayTimeInterval = [[NSDate date] timeIntervalSince1970];
NSTimeInterval timeDifference = ((todayTimeInterval - timeWhenClosedTimeInterval ) / 60);
if (timeDifference > [userDefaults doubleForKey:@"timeoutLength"]) {
NSLog(@"Timeout Hit");
} else {
NSLog(@"No Timeout");
}
return YES;
}
NSTimeInterval is expressed in seconds, not minutes.
Here’s the Apple doc where it’s described.
I’m not 100% certain what your ultimate problem with, but 500 seconds doesn’t seem like nearly enough time.
In the meantime, I wrote up some changes to your code to demo for myself:
which came up with 1400 minutes (divided by 60 minutes per hour = 24 hours).