使用 NSTimer 和 NSRunLoop 无限执行方法



我需要以每 1 分钟的间隔执行一个方法,每 30 分钟。30 分钟后,我想在接下来的 30 分钟内睡觉。

我尝试使用UIBackgroundTaskIdentifier API,它允许我只执行代码180秒,不超过这个时间。

我不想使用静默远程通知。

使用 NSTimer 和 NSRunLoop,我已经实现了这一目标,但我想知道当我提交时苹果会拒绝我的应用程序吗?

请帮助我。

- (void)applicationDidEnterBackground:(UIApplication *)application {
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
    NSTimer *loop = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(insideLoop) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:loop forMode:NSRunLoopCommonModes];
}
-(void)insideLoop {
    NSLog(@"insideLoop");
}

是的,如果您这样做,Apple 将拒绝您的应用程序。

我也想出了一种方法,可以让应用程序在后台无限期地保持清醒,但它违反了苹果的规则,咀嚼了用户的电池。

因此,

您不可能通过这种特定方法获得方法。

有一些选项用于在设备发生特定事件(如位置更改)时触发代码。后台活动的可用选项可以在此处阅读,但正如我所说,您在这里拥有的内容将无法通过。

最新更新