【发布时间】:2016-02-23 11:48:58
【问题描述】:
我需要每 30 分钟每隔 1 分钟执行一个方法。 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");
}
【问题讨论】:
-
您不能无限期地在后台运行。您的应用将在 180 秒后终止
-
当我尝试时,它正在无限期地记录。
-
当您在调试器下运行时,您的应用程序不会在超过后台时间时终止,但在您没有时它会终止。
标签: ios objective-c background nstimer nsrunloop