2015-12-22 85 views
1

我試圖讓我的iOS應用程序在後臺運行下去,(或位置採樣和診斷有關的位置),我發現這個代碼 -NSRunLoop後臺任務有效

[self.locationManager stopUpdatingLocation]; 
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.currentTimerTime target:self selector:@selector(checkLocation) userInfo:nil repeats:NO]; 
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes]; 
[[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil]; 

這是偉大的工作但我有一種感覺,蘋果不會這樣,是最佳做法?

回答

0

正如你可能已經發現,雖然這在開發中工作,它不會在生產中工作。應用程序獲得beginBackgroundTaskWithExpirationHandler的執行時間有限,無限制開發,但通常在已發佈應用程序中爲60秒,屆時將調用到期處理程序(您未在示例代碼中分配),並且預計您的應用程序將結束後臺任務使用由beginBackgroundTaskWithExpirationHandler調用返回的ID(您沒有捕獲)。不這樣做會導致你的應用程序被終止。欲瞭解更多信息,請參閱相關的問題/答案。

objective c - Proper use of beginBackgroundTaskWithExpirationHandler