0

我想在我的应用程序中有一个标签,该标签每天在特定时间(即上午 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 点)更新标签,以便在每个新的一天开始时更新。我已经尝试了几种方法来做到这一点,但没有得到想要的结果。如果有人知道这样做的方法,我将不胜感激您的意见。

4

5 回答 5

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"];
于 2013-04-25T22:20:21.320 回答
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);


}
于 2014-11-27T08:36:08.760 回答
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];
于 2013-04-25T22:50:01.627 回答
-1

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

于 2013-04-25T21:10:44.013 回答
-1

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

于 2013-04-25T21:14:12.790 回答