Ios 如何每天更新一次标签?

Ios 如何每天更新一次标签?,ios,objective-c,nsdate,Ios,Objective C,Nsdate,我希望在我的应用程序中有一个标签,每天在特定时间(即上午12点)更新一次。如果你能帮我,我会非常感激的,谢谢 这是我目前拥有的,但只有在凌晨12点到1点之间打开应用程序时,它才起作用 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH"]; NSString *stringDate = [formatter stringFromDate:[NSDate date]]

我希望在我的应用程序中有一个标签,每天在特定时间(即上午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点,这样它会随着新的一天的开始而更新。我已经尝试了几种方法,但是没有得到想要的结果。如果有人知道这样做的方法,我将非常感谢您的意见。

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

启动应用程序时,设置一个计时器,该计时器将在12:00:01 AM启动并更新标签。

如果您只需要确定上次打开应用程序的时间,您可以使用
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"];

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

例如:

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

我知道这是一篇老文章,但几天前,我需要的正是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);


}

这并不是说他们已经把应用程序打开了24小时。该应用程序根据设备的本机时钟检查时间,然后根据新的一天更新计时器。这与用户打开应用程序的时间无关。我想避免使用86400定时器,因为如果用户关闭应用程序,定时器会重置。误解大于。使用NSUserDefaults,并设置上次更新数据。在应用程序开始时,检查该值,与当前时间戳进行比较,并根据需要使用它。我会花大量时间找出我的计时器在后台停止的原因,直到我读入,以便NSTimer在后台工作3-10分钟后停止,除非您使用经批准的服务,如地图服务或外部附件。因此,不要浪费时间在NSTimer上。如何设置根据一天中的时间触发的计时器?NSTimer*time=[[NSTimer alloc]initWithFireDate:interval:target:selector:userInfo:repeats:];NSDate参数是您需要的日期(含时间),间隔为86400,目标自选择器是您要调用的方法。在“用户信息”中,将标签置于“刷新”状态,并将“重复”设置为“是”。我不一定要检查应用上次打开的时间。我正在尝试让应用程序每天在特定时间(即上午12点)更新标签。您可以从我发布的代码中确定,使用日期格式化程序检查日期是否在上午12点之后。除非你一直在运行应用程序,否则如果不检查上次打开应用程序的时间,就无法更新标签。这是针对MacOS还是iOS?iOS,特别是iPhone/iPod Touch?我认为你需要进一步澄清你想要什么,因为无论你想做什么,这似乎都是一个糟糕的方法。如果你更清楚你想要什么,我们将能够更好地为你指出正确的方向。我想在一个特定的时间每天更改一次标签的文本,如上午12点。当应用程序进入后台时,不要忘记使计时器无效。