我一直在面对一个问题,即我的应用程序不能运行超过10分钟的后台,我已经实现了后台任务,可立即获取通知。我的iPhone应用程序后台进程在10分钟后终止
我的应用程序的后台任务停止10分钟后,我已审阅的this和this解决方案,但它似乎有帮助
我的代码如下
-(void)methodBGTask :(UIApplication *)application{
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
//create new uiBackgroundTask
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
//and create new timer with async call:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//run function methodRunAfterBackground
NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(methodGetNotificatioin) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
}
}
}
-(void)methodGetNotificatioin{
//retrieve notifications from service
}
在此先感谢
这是预期的行为,有10分钟的限制,阅读文档! –
我还没有downvoted,但我猜,因为它在文档中很清楚地表明后台任务是有时间限制的,并警告不要将它们用于此类工作。 –
因为你没有做任何研究就问了一个问题,我就低估了。这在苹果文档中很清楚地解释(我自己读过) –