IOS後臺任務

http://onevcat.com/2013/08/ios7-background-multitask/html

IOS提供瞭如下多中方式處理後臺任務ios

1:beginBackgroundTaskWithExpirationHandlersession

2:特定任務的後臺處理app

3:後臺獲取ide

4:推送喚醒函數

5:後臺傳輸測試

其中後面3種方式IOS7以後才支持fetch

 

beginBackgroundTaskWithExpirationHandlerspa

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _count = 0;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(function) userInfo:nil repeats:YES];
    return YES;

}
-(void)function {
    _count++;
    NSLog(@"function:%d",_count);
}

上面代碼開啓了一個定時器,每隔一秒輸出count的值,但當咱們的應用退到前臺後定時器中止運行,咱們能夠經過函數beginBackgroundTaskWithExpirationHandler向系統請求更多地時間運行咱們的代碼,這個"更多的時間"通過測試大概在3分鐘左右code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _count = 0;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(function) userInfo:nil repeats:YES];
    return YES;

}
-(void)function {
    _count++;
    NSLog(@"function:%d",_count);
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    _identifier = [application beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"ss");
        
        [application endBackgroundTask:_identifier];
        _identifier = UIBackgroundTaskInvalid;
    }];

}

 _identifier是整型變量,beginBackgroundTaskWithExpirationHandler設置了一個超時回調函數,當超過3分鐘後後臺任務將被掛起同時執行超時函數,咱們應該在超時函數中調用endBackgroundTask,下面代碼展現了beginBackgroundTaskWithExpirationHandler更通常的調用方式

- (void)beginBackgroundTask {
    UIApplication *application = [UIApplication sharedApplication];
    _identifier = [application beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundTask];
    }];
}

- (void)endBackgroundTask {
    UIApplication *application = [UIApplication sharedApplication];
    [application endBackgroundTask:_identifier];
    _identifier = UIBackgroundTaskInvalid;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self beginBackgroundTask];
    
    //後臺任務
    
    
    
    [self endBackgroundTask];
}

 

 

後臺獲取 

後臺獲取是IOS7新增內容,它的核心做用是設定一個間隔,而後每隔一段時間喚醒應用處理相應地任務,好比咱們使用的社交軟件,能夠每一個必定時間獲取最新的信息,這樣下次咱們進入後就不須要等待刷新,使用後臺獲取的步驟以下:

1:添加應用對後臺獲取的支持,能夠在plist文件中修改UIBackgroundMode一項,增長fetch,或者在應用信息的capabilities->background modes中勾選background fetch

2:設置最小後臺獲取時間間隔

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}

若是不設置的話默認爲UIApplicationBackgroundFetchIntervalNever,表示不獲取,並且這個值表明"最小"後臺獲取時間間隔,這裏所指定的時間間隔只是表明了「在上一次獲取或者關閉應用以後,在這一段時間內必定不會去作後臺獲取」,IOS並不會爲了每個應用頻頻喚醒CPU,具體喚醒時間得看系統調度,設置爲UIApplicationBackgroundFetchIntervalMinimum表示儘量的對咱們的應用進行後臺喚醒,這樣設置的缺點是耗電。

3:實現application:performFetchWithCompletionHandler 

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cnblogs.com/zanglitao/"]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error) {
            NSLog(@"%@",[error localizedDescription]);
            completionHandler(UIBackgroundFetchResultFailed);
        } else {
            ////更新UI
            NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
            completionHandler(UIBackgroundFetchResultNewData);
        }

    }];
    
    [task resume];
}

與beginBackgroundTaskWithExpirationHandler類似,系統提供了回調completionHandler,用於通知系統任務執行完畢

 

推送喚醒

1:在UIBackgroundModes添加remote-notification

2:更改推送的payload:須要在payload中添加content-available,並設置爲1

3:實現推送喚醒代碼

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

代碼實現與上一種方式一致,任務實現完須要調用回調方法通知系統

 

後臺傳輸

IOS7中提供了NSURLSession替代NSURLConnection實現數據傳輸,NSURLSession的用法以前博文有提

相關文章
相關標籤/搜索