在后台状态下安排重复任务。 (IOS)

时间:2012-05-01 20:28:29

标签: iphone objective-c ios ipad scheduled-tasks

如果我的研究在您的应用程序处于后台时是正确的,那么它实际上已被暂停。 Apple支持的后台任务方法是位置,音频和& SIP类型的应用程序是正确的吗?

在后台运行方法时,是否有Apple支持的方法每60秒运行一次,我需要检查服务器的状态。以下代码执行我需要的操作,但在10分钟后停止,我相信这是Apple支持的另一种方法。任何关于这是否可行的建议或确认都会有所帮助。

下面的代码只创建一个每10秒运行一次的任务并更新徽章计数。

此致

- (void)viewDidLoad
{
    [super viewDidLoad];

    count=0;
    counterTask = [[UIApplication sharedApplication]
                   beginBackgroundTaskWithExpirationHandler:^{
                       // If you're worried about exceeding 10 minutes, handle it here
                  }];
    theTimer=[NSTimer scheduledTimerWithTimeInterval:5
                                             target:self
                                           selector:@selector(countUp)
                                            userInfo:nil
                                            repeats:YES];
}

- (void)countUp {
    NSLog(@"%s - %d", __FUNCTION__, count + 1 );

    if (count==1000) {
        [theTimer invalidate];
        [theTimer release];
        [[UIApplication sharedApplication] endBackgroundTask:counterTask];
    } else {
        count++;
        NSString *currentCount;
        currentCount=[[NSString alloc] initWithFormat:@"%d",count];
         [currentCount release];
        [UIApplication sharedApplication].applicationIconBadgeNumber = count;

    }
}

1 个答案:

答案 0 :(得分:0)

不幸的是,我在我的应用程序中也使用了这个10分钟的后台任务,而且从我做过的研究中,这是在后台运行方法的唯一方法。希望能回答你的问题

相关问题