Jon
Jon

Reputation: 4732

iOS timeout functionality not working properly

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;
}

Upvotes: 1

Views: 1056

Answers (2)

Michael Dautermann
Michael Dautermann

Reputation: 89509

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:

NSDate * yesterday = [[NSDate date] dateByAddingTimeInterval: (-1 * 60 * 60 * 24 )];
NSTimeInterval yesterdayTimeInterval = [yesterday timeIntervalSince1970];
NSTimeInterval todayTimeInterval = [[NSDate date] timeIntervalSince1970];

// this properly converts timeDifference in seconds to minutes
NSTimeInterval timeDifference = ((todayTimeInterval - yesterdayTimeInterval ) / 60);
NSLog( @"time difference is %4.2f", timeDifference );

which came up with 1400 minutes (divided by 60 minutes per hour = 24 hours).

Upvotes: 1

Michael Frederick
Michael Frederick

Reputation: 16714

My guess is there is some error with setting the NSUserDefaults values, or an error with your timeDifference calculation. Add this line and make sure you are actually setting the timeout length to 500:

NSLog(@"Timeout length: %f, close time: %f, time difference: %f", [userDefaults doubleForKey:@"timeoutLength"], [userDefaults doubleForKey:@"Close Time"], timeDifference);

Upvotes: 1

Related Questions