我一直在面對一個問題,即我的應用程序不能運行超過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,但我猜,因爲它在文檔中很清楚地表明後臺任務是有時間限制的,並警告不要將它們用於此類工作。 –
因爲你沒有做任何研究就問了一個問題,我就低估了。這在蘋果文檔中很清楚地解釋(我自己讀過) –