2013-10-14 38 views
0

我想在一定的時間間隔內發出url請求(例如,每10分鐘的應用程序應該進行url調用並獲取一些json數據)。應用程序應該能夠在後臺運行時執行此操作。這可以做到嗎?如果是這樣,這是否違反了Apple的服務條款?有任何限制嗎?在後臺運行URL請求

+0

是的,它可以做...我不認爲蘋果會拒絕應用程序,因爲有很多應用程序支持後臺模式運行.. –

+0

這將是很好,如果你通過應用程序審查指南..! –

回答

3

使用ios 7添加了可以在後臺運行的新應用程序列表。它們是:

1. Apps that play audible content to the user while in the background, such as a music player app 
2. Apps that record audio content while in the background. 
3. Apps that keep users informed of their location at all times, such as a navigation app 
4. Apps that support Voice over Internet Protocol (VoIP) 
5. Apps that need to download and process new content regularly 
6. Apps that receive regular updates from external accessories 

我們只需要在info.plist中聲明app支持的後臺任務。我們需要將 的UIBackgroundModes密鑰添加到我們的應用的信息。 plist。之後,你可以正常使用你的計時器,如:

UIBackgroundTaskIdentifier bgTask; 

UIApplication *app = [UIApplication sharedApplication]; 

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    [app endBackgroundTask:bgTask]; 
}]; 

self.timer = [NSTimer scheduledTimerWithTimeInterval:600 target:self 
selector:@selector(startServices) userInfo:nil repeats:YES]; 

希望它能幫助你。

+0

非常感謝您的詳細解答 – sajaz

0

在基本級別:只需調度NSTimer並在定時器功能上啓動URL請求即可。如果你的目標是iOS7,那麼確保你使用的是NSURLSession。但重要的一點是要知道你是否想在後臺做這件事。如果是的話,你必須找到更多關於iOS背景模式的信息,因爲你將無法在這種模式下維護定時器。沒有什麼會導致拒絕。

+0

感謝您的詳細解答。是的,我需要應用程序在後臺模式下運行。實際要求是,我想跟蹤用戶當前位置並每10分鐘發送一次服務器 – sajaz