如何每天更新一次标签?

时间:2013-04-25 21:06:14

标签: ios objective-c nsdate

我希望我的应用中有一个标签,可以在特定时间(即上午12点)每天更新一次。如果你能帮助我,我真的很感激,谢谢。

这是我目前拥有的,但它只适用于12点到1点之间打开的应用程序。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH"];

NSString *stringDate = [formatter stringFromDate:[NSDate date]];

if ([stringDate isEqualToString:@"00"]) {

NSLog(@"its working");


[self changeLabel];

}

为了澄清,我试图每天更改一次标签的文字。我希望在特定时间(例如上午12点)更新标签,以便在每个新的一天开始时更新。我已经尝试了几种方法,但没有得到预期的结果。如果有人知道如何做到这一点我会很感激您的意见。

5 个答案:

答案 0 :(得分:1)

如果您只需要确定上次打开应用程序的时间,可以使用NSUserDefaults保存日期,然后检查是否已经过了适当的秒数(86400 = 1天)。

以下内容应指向正确的方向:

    //gather last time app was opened
    NSDate *lastUpdated = [[NSUserDefaults standardUserDefaults] objectForKey:@"app_last_updated"];

    //check date is valid
    if(lastUpdated)
    {
        //determine number of seconds that have passed
        NSTimeInterval timeInterval = [lastUpdated timeIntervalSinceNow];

        //check time interval is greater than 1 day
        if(timeInterval > 86400)
        {
            //update label
        }
    }

    //save current time
    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"app_last_updated"];

答案 1 :(得分:0)

这取决于你想做什么。在iOS上,很可能您的应用程序不是一直在前台运行。所以我只会在applicationDidBecomeActive:中的应用程序处于活动状态时更新标签。然后我会在我想要的特定日期/时间创建一个计时器。

例如:

NSDate *d = // create the next date/time
updateTimer_ = [[NSTimer alloc] initWithFireDate: d
                          interval: 0
                          target: self
                          selector:@selector(updateLabel:)
                          userInfo:nil repeats:NO];

NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:updateTimer_ forMode: NSDefaultRunLoopMode];

答案 2 :(得分:0)

我知道这是一个老帖子,但几天前我只需要Dave123提出的问题,我在网上搜索了很多内容后想出了这个解决方案:

- (void)applicationDidBecomeActive:(UIApplication *)application {

    NSLog(@"applicationDidBecomeActive");

    self.masterviewController = [[PBMasterViewController alloc] init];

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    NSLog(@"Seconds --------> %.f",[[NSDate date] timeIntervalSinceDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"OLD_DATE"]]);
    NSLog(@"old date: %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"OLD_DATE"]);

    // 24 hours must elapse between each update check
    if ([[NSDate date] timeIntervalSinceDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"OLD_DATE"]] > 86400) {

        NSLog(@"Time elapsed since last update is MORE than 1 day. Check for updates.");
        [self.masterviewController checkForUpdates];

    } else {

        NSLog(@"Time elapsed since last update is LESS than 1 day. Don't check for updates.");

    }

    // Here I convert the seconds into hours with two decimals, if you want to show the time elapsed in a UILabel
    NSString *stringCheckInterval = [NSString stringWithFormat:@"%.02f hours since last update", [[NSDate date] timeIntervalSinceDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"OLD_DATE"]] / 60 / 60];
    NSLog(@"%@", stringCheckInterval);


}

答案 3 :(得分:-1)

是否有人将应用程序打开超过24小时? 无论如何,你可以简单地使用NSTimer和86400作为延迟时间。

答案 4 :(得分:-1)

当您启动应用程序时,请设置一个计时器,该计时器将在凌晨12:00:01开始并更新您的标签。