1. 程式人生 > >iOS NSURLSession 實現網路請求-檔案下載-上傳-後臺下載

iOS NSURLSession 實現網路請求-檔案下載-上傳-後臺下載

 *  會話NSURLSession

    NSURLConnection通過全域性狀態來管理cookies, 認證資訊等公共資源, 如果兩個連線需要使用不同的資源配置情況時就無法解決,
    這個問題在NSURLSession可以解決, NSURLSession同時對應著多個連線, 會話通過工廠方法來建立, 同一個會話中使用相同的狀態資訊, NSURLSession支援程序三種會話:
    1. defaultSessionConfiguration : 程序內會話 (預設會話), 用來硬碟來快取資料.
    2. ephemeralSessionConfiguration : 臨時的程序內會話(記憶體), 不會將cookie, 快取儲存到本地, 只會放到記憶體中, 當應用程式退出後資料也會消失
    3. backgroundSessionConfiguration : 後臺會話, 相比預設會話, 該會話會在後臺開啟一個縣城進行網路資料處理

    精準的控制任務的取消, 掛起, 恢復

1. 請求資料

<span style="font-size:18px;">#pragma mark --- 請求資料 request --- session ---- sessionDataTask

- (void)loadData
{
    // 1. 建立url
    NSString *urlString = [NSString stringWithFormat:@"url"];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    // 2. 建立請求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 3. 建立會話 (使用單例初始化, 啟動任務)
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    // 會話建立任務
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (!error) {
            NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@", dataStr);
        } else {
            NSLog(@"error is %@", error.localizedDescription);
        }
        
    }];
    
    // 恢復執行緒, 啟動任務
    [dataTask resume];
}
</span>

2. 檔案上傳

<span style="font-size:18px;">#pragma mark ----- 檔案上傳
- (void)upDataFile
{
    /**
     *  檔案上傳的時候需要設定請求頭中Content-Type型別, 必須使用URL編碼, 
     
     application/x-www-form-urlencoded:預設值,傳送前對所有傳送資料進行url編碼,支援瀏覽器訪問,通常文字內容提交常用這種方式。
     multipart/form-data:多部分表單資料,支援瀏覽器訪問,不進行任何編碼,通常用於檔案傳輸(此時傳遞的是二進位制資料) 。
     text/plain:普通文字資料型別,支援瀏覽器訪問,傳送前其中的空格替換為“+”,但是不對特殊字元編碼。
     application/json:json資料型別,瀏覽器訪問不支援 。
     text/xml:xml資料型別,瀏覽器訪問不支援。
     
     multipart/form-data 必須進行設定,
     */
    // 1. 建立URL
    NSString *urlStr = @"url";
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlStr];
    // 2. 建立請求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 設定請求的為POST
    request.HTTPMethod = @"POST";
    
    // 3.構建要上傳的資料
    NSString *path = [[NSBundle mainBundle] pathForResource:@"123" ofType:@"123"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    // 設定request的body
    request.HTTPBody = data;
    
    // 設定請求 Content-Length
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];
    // 設定請求 Content-Type
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",@"Xia"] forHTTPHeaderField:@"Content-Type"];
    
    // 4. 建立會話
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!error) {
            // 上傳成功
        }else {
            // 上傳失敗, 列印error資訊
            NSLog(@"error --- %@", error.localizedDescription);
        }
    }];
    // 恢復執行緒 啟動任務
    [uploadTask resume];
    
}</span>

3. 檔案下載

<span style="font-size:18px;">#pragma mark --- 檔案下載
/**
 *  使用NSURLSessionDownloadTask下載檔案過程中注意:
    下載檔案之後會自動儲存到一個臨時目錄中, 需要自己將檔案重新放到其他指定的目錄中
 */
- (void)downLoadFile
{
    // 1. 建立url
    NSString *urlStr =[NSString stringWithFormat:@"%@", @"url"];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *Url = [NSURL URLWithString:urlStr];
    
    // 建立請求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url];
    
    // 建立會話
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDownloadTask *downLoadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!error) {
            // 下載成功
            // 注意 location是下載後的臨時儲存路徑, 需要將它移動到需要儲存的位置
            NSError *saveError;
            // 建立一個自定義儲存路徑
            NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *savePath = [cachePath stringByAppendingPathComponent:@"fileName"];
            NSURL *saveURL = [NSURL fileURLWithPath:savePath];
            
            // 檔案複製到cache路徑中
            [[NSFileManager defaultManager] copyItemAtURL:location toURL:saveURL error:&saveError];
            if (!saveError) {
                NSLog(@"儲存成功");
            } else {
                NSLog(@"error is %@", saveError.localizedDescription);
            }
        } else {
            NSLog(@"error is : %@", error.localizedDescription);
        }
    }];
    // 恢復執行緒, 啟動任務
    [downLoadTask resume];
    
}
</span>

4. 檔案的取消下載,掛起,繼續下載

<span style="font-size:18px;">#pragma mark -- 取消下載
-(void)cancleDownLoad
{
    [_downloadTask cancel];
}
#pragma mark --- 掛起下載
- (void)suspendDownload
{
    [_downloadTask suspend];
}
#pragma mark ---- 恢復繼續下載
- (void)resumeDownLoad
{
    [_downloadTask resume];
    

}</span>

5. NSURLSession的代理方法

<span style="font-size:18px;">#pragma mark ---- downLoadTask 代理方法
// 下載過程中 會多次呼叫, 記錄下載進度
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    // 記錄下載進度
    
}

// 下載完成
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    NSError *error;
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *savePath = [cachePath stringByAppendingPathComponent:@"savename"];
    
    NSURL *saveUrl = [NSURL fileURLWithPath:savePath];
    // 通過檔案管理 複製檔案
    [[NSFileManager defaultManager] copyItemAtURL:location toURL:saveUrl error:&error];
    if (error) {
        NSLog(@"Error is %@", error.localizedDescription);
    }
}

// 當呼叫恢復下載的時候 觸發的代理方法 [_downLoadTask resume]
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
    
    
}


#pragma mark --- 任務完成, 不管是否下載成功
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    
}
#pragma mark --- session 後臺下載完成 之後的操作 (本地通知 或者 更新UI)
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
    AppDelegate *appdelgate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    if (appdelgate.backgroundSessionCompletionHandler) {
        void (^completionHandle)() = appdelgate.backgroundSessionCompletionHandler;
        appdelgate.backgroundSessionCompletionHandler = nil;
        completionHandle();
    }
}</span>

6. 後臺下載

 NSURLSession 支援程式的後臺下載和上傳, 蘋果官方將其稱之程序之外的上傳和下載, 這些任務都交給後臺守護執行緒完成, 而不是應用本身, 及時檔案在下載和上傳過程中崩潰了也可以繼續執行(如果使用者強制關閉程式的話, NSURLSession會斷開連線)
 *
 *  為了提高使用者體驗, 下載過程中進度條會一直重新整理進度,
    當程式進入後臺後, 事實上任務是交給iOS系統來排程的, 具體什麼時候下載完成就不知道了,
    如果下載完成之後, 檔案下載進度應該在100位置, 由於程式已經在後臺無法更新程式UI,
    此時通過應用程式代理 (AppDelegate)方法進行UI更新
 
   

當NSURLSession在後臺開啟幾個任務之後, 如果有其他幾個任務完成後系統會呼叫應用程式的
   

-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier 

completionHandler:(void (^)())completionHandler代理方法
   

此方法會包含一個competionHandler (此操作表示應用中完成所有處理工作), 通常我們會儲存此物件,
   

直到最後一個任務完成, 此時重新通過會話標識(sessionConfig中設定的)找到相應的會話並呼叫NSURLSession的

-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session代理方法, 在這個方法中通常可以

進行UI更新,並呼叫completionHandler通知系統已經完成所有的操作

<span style="font-size:18px;">#pragma mark --- 後臺下載
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
{
    self.backgroundSessionCompletionHandler = completionHandler;
}

- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
    AppDelegate *appdelgate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    if (appdelgate.backgroundSessionCompletionHandler) {
        void (^completionHandle)() = appdelgate.backgroundSessionCompletionHandler;
        appdelgate.backgroundSessionCompletionHandler = nil;
        completionHandle();
    }
}</span>