我在应用中有一个计时器。要重现此问题,用户应启动此计时器,然后将应用程序输入后台或关闭屏幕。该应用程序最多可运行5分钟,然后被系统杀死。用户重新打开屏幕后,可以看到主屏幕。如果再次启动应用程序,它将从头开始,但不会从用户所在的位置开始,然后关闭屏幕。
我尝试使用AppDelegate中的以下解决方案解决此问题:
- (void)methodToRepeatEveryOneSecond
{
if (self.flag) {
dispatch_queue_t q_background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, q_background, ^(void){
[self methodToRepeatEveryOneSecond];
});
}
}
-(void)applicationDidEnterBackground:(UIApplication *)application{
NSUserDefaults* def = [[NSUserDefaults alloc] init];
if ( [[def valueForKey:@"status" ] boolValue]) {
self.flag = true;
dispatch_queue_t q_background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(q_background, ^{
[self methodToRepeatEveryOneSecond];
});
}
}
-(void)applicationWillEnterForeground:(UIApplication *)application{
self.flag = false;
}
但是app在后台~5分钟后仍然停止工作。
当我在USB有线电视应用程序的帮助下测试此解决方案时,即使长达30分钟也能在后台运行,但在后台工作约4分钟后,我可以在控制台中看到以下日志(这里是一个部分内容):
2017-07-26 17:43:14.496379+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16df86530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.496493+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e1b6530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.496790+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e012530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.497065+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16df86530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.497188+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e242530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2017-07-26 17:43:14.497476+0300 MusicMonitor[486:339431] Can't endBackgroundTask: no background task exists with identifier 16e242530, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
但是如果要在testflight下载的帮助下测试相同的版本,我会遇到问题,我在上面写的(显示主屏幕)。
此外,我尝试在我的Info.plist文件中使用UIBackgroundModes键中的位置支持,它工作正常,但beta应用程序审核因“«Guideline 2.5.4»”而被拒绝。
请给我建议,我可以用什么来解决这个问题
答案 0 :(得分:-1)
当应用程序处于后台时,您的应用会终止自身3分钟的不活动状态,并且您放弃了所有优势,而您的应用无法执行任何操作,但会收听通知(如果您已实施通知)
如果您试图禁止应用程序终止,您可以让它在该背景中运行无声mp3,这样您的应用程序始终处于活动状态(耗尽电池加上将被拒绝)。
但是,如果用户双击主页按钮并向上滑动您的应用程序(终止它)。你无能为力!
希望这有帮助!