在iOS系統,App的前台運行和後台運行,行為是不同的,iOS作業系統對後台運行做了諸多限制,為了能夠讓系統運行更流程和更省電。
App的狀態如:
對於後台運行,首先需要確定裝置是否支援多任務,在iOS4.0 之前是否沒辦法做到多任務的,不過現在iOS4.0的裝置已經很少了。
UIDevice* device = [UIDevice currentDevice]; BOOL backgroundSupported = NO; if ([device respondsToSelector:@selector(isMultitaskingSupported)]) backgroundSupported = device.multitaskingSupported;
第一種,使用beginBackgroundTaskWithExpirationHandler:在
applicationDidEnterBackground裡調用
- (void)applicationDidEnterBackground:(UIApplication *)application{ // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 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. // your code NSLog(@" %f",application.backgroundTimeRemaining); [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; });}
第二種,使用本地系統通知
- (void)scheduleAlarmForDate:(NSDate*)theDate { UIApplication* app = [UIApplication sharedApplication]; NSArray* oldNotifications = [app scheduledLocalNotifications]; // Clear out the old notification before scheduling a new one. if ([oldNotifications count] > 0) [app cancelAllLocalNotifications]; // Create a new notification. UILocalNotification* alarm = [[UILocalNotification alloc] init]; if (alarm) { alarm.fireDate = theDate; alarm.timeZone = [NSTimeZone defaultTimeZone]; alarm.repeatInterval = 0; alarm.soundName = @"alarmsound.caf"; alarm.alertBody = @"Time to wake up!"; [app scheduleLocalNotification:alarm]; } }
第三種調用系統指定的後台運行任務,有一下這些,例如GPS 音樂等
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]; NSError *error; NSURLResponse *response; NSData *data= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
可正常收到response,暫時解決了個問題。但對於有數個請求,執行效率是一個問題。