iOS後台網路任務

來源:互聯網
上載者:User

在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,暫時解決了個問題。但對於有數個請求,執行效率是一個問題。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.