我正在努力使它正确工作,但它似乎总是提前174秒(2.9分钟)结束。我一直在在线上学习关于如何使用beginBackgroundTaskWithExpirationHandler的每一个教程,我没有发现我的代码有什么问题。我要这个在8.5分钟结束。在调用到期处理程序之前,endBackgroundTask方法甚至都不会得到调用。这有什么问题吗?
- (void)applicationDidEnterBackground:(UIApplication *)application {
if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
{
NSLog(@"Multitasking Supported");
backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^ {
NSLog(@"Terminated");
//Clean up code. Tell the system that we are done.
[application endBackgroundTask: backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}];
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(count:)
userInfo:nil
repeats:YES];
}
else
{
NSLog(@"Multitasking Not Supported");
}
}
-(void)turnOffDesktops:(NSTimer *)time {
//do smth
if(count < (60*8.5)){
NSLog(@"%d",count);
count++;
}else{
[application endBackgroundTask: backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
[timer invalidate];
timer = nil;
count = 0;
}}
发布于 2013-12-21 20:18:58
苹果从来没有承诺过你将被允许多长时间来执行你的背景任务。历史上(直到iOS7),应用程序通常在后台运行10分钟。不再是这种情况了!关注WWDC 2013年关于背景的视频。随着在NSURLSession中添加了新的下载和上传API (能够在外部专用守护进程上安排下载和上载任务),苹果大大减少了允许的后台时间。他们这么做是因为这个API一直是用来下载和上传的,而不是后台的任意任务。
您可以通过查询- [UIApplication backgroundTimeRemaining]来确定背景中剩下的时间量。您可以使用它来安排您的代码在最晚可能的时候开始。
https://stackoverflow.com/questions/20723112
复制相似问题