2016-02-09 60 views
0

我們可以在ios中寫入後臺服務嗎?它會在延遲一段時間後(例如1分鐘)持續在後臺運行。在ios中寫入後臺服務

我有一個應用程序,每分鐘後我想爲它寫後臺服務提取數據。

獨立於應用程序運行與否,雖然應用程序不在內存中,但服務應在1分鐘後連續運行。

我已經寫在android應用程序相同的服務,但我不知道它是可能的或不在ios中。

回答

0

那麼,實際上它取決於你想要執行的任務。主要觀點是你無法永久執行背景中的任何任務。在你需要的時間間隔。 你可以像在下載後臺執行一些任務,等你需要- (void)applicationDidEnterBackground:(UIApplication *)application

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{ 
     // 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; 
    }); 
} 

使用dispatch_async喜歡的東西對於更像是有限的任務詳細信息,你可以找到在以下鏈接蘋果的文檔。

BackgroundExecution

您還可以得到更多的幫助,從下面的鏈接就後臺服務。

running-background-services-in-ios