2013-03-07 129 views

回答

3

您可以在後臺通過使用此代碼爲特定的時間

UIBackgroundTaskIdentifier bgTask = 0; 
    UIApplication *app = [UIApplication sharedApplication]; 
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     [app endBackgroundTask:bgTask]; 
    }]; 
    self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self 
                 selector:@selector(startLocationServices) userInfo:nil repeats:YES]; 

執行任務,請參閱本: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

我認爲這將幫助ü。 :)

+0

謝謝....我通過閱讀蘋果文檔得出結論:上傳過程可以在應用程序轉到背景後執行最長10分鐘, – 2013-03-07 09:46:55

+0

歡迎您@KhushbuPatel ..快樂編碼:) – shivam 2013-03-07 10:29:56

1

應用程序可以請求在關閉後最多10分鐘後在後臺運行,以便它可以完成長時間運行的任務。只有一些進程被允許在後臺運行。見實現長時間運行的後臺任務部分in this reference.

如果您的應用允許的話,你可以試試下面的代碼:

- (void)applicationDidEnterBackground:(UIApplication *)application 

{ 

    UIBackgroundTaskIdentifier bgTask; 
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ 

     // Clean up any unfinished task business by marking where you 

     // stopped or ending the task outright. 

     [application endBackgroundTask:bgTask]; 

     bgTask = UIBackgroundTaskInvalid; 

    }]; 



    // Start the long-running task and return immediately. 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 



     // Do the work associated with the task, preferably in chunks. 



     [application endBackgroundTask:bgTask]; 

     bgTask = UIBackgroundTaskInvalid; 

    }); 

} 

,如果你想知道你的程序還剩多少時間來運行

NSTimeInterval ti = [[UIApplication sharedApplication]backgroundTimeRemaining]; 
NSLog(@"Remaining Time: %f", ti); // just for debug 

更多裁判去與這個reference PDF(page 60)

相關問題