【问题标题】:iOS timeout functionality not working properlyiOS 超时功能无法正常工作
【发布时间】:2011-12-03 03:29:54
【问题描述】:

我添加了这段代码,它在我的应用程序中用作自动超时。 userDefaults doubleForKey:@"timeoutLength" 应该以分钟为单位。例如,如果值为 500,则应表示 500 分钟。

我似乎一直在进入 timout 循环,即使它实际上还没有超过 500 分钟。我的代码有什么问题吗?可能是分钟/秒错误等。

    [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");
    }

编辑:

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

【问题讨论】:

    标签: objective-c ios cocoa-touch timeout


    【解决方案1】:

    NSTimeInterval 以 表示,而不是分钟。

    Here's the Apple doc where it's described

    我不能 100% 确定您的最终问题是什么,但 500 秒似乎还不够。

    与此同时,我对您的代码进行了一些更改以供自己演示:

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

    得出 1400 分钟(除以每小时 60 分钟 = 24 小时)。

    【讨论】:

    • 我们的目标是检查 timeClose 和 now 之间的差异是否大于 timeoutLength。那么我的差距是分钟还是秒?
    • 是的,我觉得很像。我还为您编辑了演示的答案。
    • 我刚刚根据您的回答添加了新更新的代码,如果您可以查看的话。当应用程序关闭时,我将时间戳保存在 NSUserDefauts 中。然后下次启动应用程序时,我检查时间差是否大于autologout_idle_timeout userdefaults 值。
    • 我认为 autologout_idle_timeoutdouble 而不是 NSDate 存在问题,但数字总是这样给我,例如它可能是 100、500 或 1000等。
    • 我马上看到的一件事是您将Close Time 默认设置为 NSDate,然后将其读取为double。你应该通过objectForKey阅读它。
    【解决方案2】:

    我的猜测是设置 NSUserDefaults 值有一些错误,或者你的 timeDifference 计算有错误。添加此行并确保您实际上将超时长度设置为 500:

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

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 2017-09-23
      • 2015-10-24
      • 2013-11-06
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多