2011-12-01 129 views
1

我正在實現iPhone應用程序。我通過在應用程序中使用異步請求從服務器下載數據。如果按下主頁按鈕,我的應用程序委託不會直接調用。從服務器完成數據下載後,應用程序委託方法正在調用。那麼,如何在我的應用程序中獲得立即的委託調用。當應用程序進入後臺時,應用程序代理不會調用

任何幫助,可以體會到。

回答

1

您正在實施哪種委託方法?你能在這裏發佈你已經嘗試過的代碼嗎?請注意,一旦您的應用程序移至後臺,您只能運行短期任務以延長電池壽命。

這個例子是蘋果公司提供:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    UIApplication* app = [UIApplication sharedApplication]; 

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     // Clean up any unfinished task business by marking where you. 
     // stopped or ending the task outright. 
     [app 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. 

     [app endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }); 
} 

如果要執行長正在運行的任務,需要特殊權限,即使如此,也只能執行動作的某個子集。他們在這裏討論:

iOS App Programming Guide: App States and Multitasking

相關問題