IOS 開發 網路詳解(二)

來源:互聯網
上載者:User

標籤:syn   web   odi   串連   current   nsca   表單提交   授權   dict   

 

 

NSURLSesstion

 

NSURLConnection在IOS2出現,在iOS9被宣布棄用,NSURLSession從13年發展到現在,終於迎來了它獨步江湖的時代.NSURLSession是蘋果在iOS7後為HTTP資料轉送提供的一系列介面,比NSURLConnection強大,坑少,好用.今天從使用的角度介紹下.

 

使用NSURLSession,攏共分兩步:第一步 通過NSURLSession的執行個體建立task第二部 執行task既然兩步裡面都出現了task,就先說說它吧.NSURLSessionTask可以簡單理解為任務:如資料請求任務,下載任務,上傳任務and so on.我們使用的是他的子類們:NSURLSessionTask(抽象類別)NSURLSessionDataTaskNSURLSessionUploadTaskNSURLSessionDownloadTask從這幾個子類的名字就可以大概猜出他們的作用了.接下來我們就從不同類型的任務出發,來使用session.
NSURLSessionDataTask字面上看是和資料相關的任務,但其實dataTask完全可以勝任downloadTask和uploadTask的工作.這可能也是我們使用最多的task種類.簡單GET請求如果請求的資料比較簡單,也不需要對返回的資料做一些複雜的操作.那麼我們可以使用帶block// 捷徑獲得session對象NSURLSession *session = [NSURLSession sharedSession];NSURL *url = [NSURL URLWithString:@"http://www.daka.com/login?username=daka&pwd=123"];// 通過URL初始化task,在block內部可以直接對返回的資料進行處理NSURLSessionTask *task = [session dataTaskWithURL:url                               completionHandler:^(NSData *data, NSURLResponse *response, NSError error) {    NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);}];// 啟動任務[task resume];Tips:所有類型的task都要調用resume方法才會開始進行請求.
簡單POST請求POST和GET的區別就在於request,所以使用session的POST請求和GET過程是一樣的,區別就在於對request的處理.NSURL *url = [NSURL URLWithString:@"http://www.daka.com/login"];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];request.HTTPMethod = @"POST";request.HTTPBody = [@"username=daka&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];NSURLSession *session = [NSURLSession sharedSession];// 由於要先對request先行處理,我們通過request初始化taskNSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }];[task resume];NSURLSessionDataDelegate代理方法
NSURLSessionDataDelegate代理方法NSURLSession提供了block方式處理返回資料的簡便方式,但如果想要在接收資料過程中做進一步的處理,仍然可以調用相關的協議方法.NSURLSession的代理方法和NSURLConnection有些類似,都是分為接收響應、接收資料、請求完成幾個階段.// 使用代理方法需要設定代理,但是session的delegate屬性是唯讀,要想設定代理只能通過這種方式建立sessionNSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]                                                      delegate:self                                                 delegateQueue:[[NSOperationQueue alloc] init]];// 建立任務(因為要使用代理方法,就不需要block方式的初始化了)NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.daka.com/login?userName=daka&pwd=123"]]];// 啟動任務[task resume];//對應的代理方法如下:// 1.接收到伺服器的響應- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {    // 允許處理伺服器的響應,才會繼續接收伺服器返回的資料    completionHandler(NSURLSessionResponseAllow);}// 2.接收到伺服器的資料(可能調用多次)- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {    // 處理每次接收的資料}// 3.請求成功或者失敗(如果失敗,error有值)- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {    // 請求完成,成功或者失敗的處理}
簡單下載NSURLSessionDownloadTask同樣提供了通過NSURL和NSURLRequest兩種方式來初始化並通過block進行回調的方法.下面以NSURL初始化為例:SURLSession *session = [NSURLSession sharedSession];NSURL *url = [NSURL URLWithString:@"http://www.daka.com/resources/image/icon.png"] ;NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {    // location是沙箱中tmp檔案夾下的一個臨時url,檔案下載後會存到這個位置,由於tmp中的檔案隨時可能被刪除,所以我們需要自己需要把下載的檔案挪到需要的地方    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];    // 剪下檔案    [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:path] error:nil];}];    // 啟動任務    [task resume];簡單的上傳NSURLSessionUploadTask *task =[[NSURLSession sharedSession] uploadTaskWithRequest:request                                           fromFile:fileName                                  completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}];和 [self.session uploadTaskWithRequest:request                            fromData:body                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"-------%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }];

斷點續傳

 

 

其他此外,task們自身有都擁有下面幾個方法- (void)suspend;- (void)resume;- (void)cancel;suspend可以讓當前的任務暫停resume方法不僅可以啟動任務,還可以喚醒suspend狀態的任務cancel方法可以取消當前的任務,你也可以向處於suspend狀態的任務發送cancel訊息,任務如果被取消便不能再恢複到之前的狀態.

 

NSURLSessionConfiguration簡單地說,就是session的配置資訊.如:NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];// 逾時時間config.timeoutIntervalForRequest = 10;// 是否允許使用蜂窩網路(後台傳輸不適用)config.allowsCellularAccess = YES;// 還有很多可以設定的屬性有沒有發現我們使用的Configuration都是預設配置:[NSURLSessionConfiguration defaultSessionConfiguration],其實它的配置有三種類型:+ (NSURLSessionConfiguration *)defaultSessionConfiguration;+ (NSURLSessionConfiguration *)ephemeralSessionConfiguration;+ (NSURLSessionConfiguration *)backgroundSessionConfigurationWithIdentifier:(NSString *)identifier表示了NSURLSession幾種不同的工作模式.預設的配置會將緩衝儲存在磁碟上,第二種瞬時會話模式不會建立持久性儲存的緩衝,第三種後台會話模式允許程式在後台進行上傳下載工作.除了支援任務的暫停和斷點續傳,我覺得NSURLSession之於NSURLConnection的最偉大的進步就是支援後台上傳下載任務,這又是一個可以深入討論的話題.但在這方面我還沒有進行深入的研究,待後續瞭解之後另行開貼.

 

ASI

 同步請求:

 

發起一個同步請求同步意為著線程阻塞,在主線程中使用此方法會使應用Hang住而不響應任何使用者事件。所以,在應用程式設計時,大多被用在專門的子線程增加使用者體驗,或用非同步請求代替(下面會講到)。- (IBAction)grabURL:(id)sender{  NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  [request startSynchronous];  NSError *error = [request error];  if (!error) {    NSString *response = [request responseString];  }}

 

 

非同步請求:

 

建立一個非同步請求非同步請求的好處是不阻塞當前線程,但相對於同步請求略為複雜,至少要添加兩個回調方法來擷取非同步事件。下面非同步請求程式碼完成上面同樣的一件事情:- (IBAction)grabURLInBackground:(id)sender{   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];   [request setDelegate:self];   [request startAsynchronous];} - (void)requestFinished:(ASIHTTPRequest *)request{   // Use when fetching text data   NSString *responseString = [request responseString];    // Use when fetching binary data   NSData *responseData = [request responseData];} - (void)requestFailed:(ASIHTTPRequest *)request{   NSError *error = [request error];}

 

隊列請求:

隊列請求提供了一個對非同步請求更加精準豐富的控制。如,可以設定在隊列中,同步請求的串連數。往隊列裡添加的請求執行個體數大於maxConcurrentOperationCount時,請求執行個體將被置為等待,直到前面至少有一個請求完成並出列才被放到隊列裡執行。也適用於當我們有多個請求需求按順序執行的時候(可能是業務上的需要,也可能是軟體上的調優),僅僅需要把maxConcurrentOperationCount設為“1”。- (IBAction)grabURLInTheBackground:(id)sender{   if (![self queue]) {      [self setQueue:[[[NSOperationQueue alloc] init] autorelease]];   }    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];   [request setDelegate:self];   [request setDidFinishSelector:@selector(requestDone:)];   [request setDidFailSelector:@selector(requestWentWrong:)];   [[self queue] addOperation:request]; //queue is an NSOperationQueue} - (void)requestDone:(ASIHTTPRequest *)request{   NSString *response = [request responseString];} - (void)requestWentWrong:(ASIHTTPRequest *)request{   NSError *error = [request error];}建立NSOperationQueue,這個Cocoa架構的執行任務(NSOperation)的任務隊列。我們通過ASIHTTPRequest.h的源碼可以看到,
此類本身就是一個NSOperation的子類。也就是說它可以直接被放到"任務隊列"中,並被執行。上面的代碼隊了隊列的建立與添加操作外,其它代碼與上一例一樣。

 

上傳:

向伺服器端上傳資料ASIFormDataRequest ,類比 Form表單提交,其提交格式與 Header會自動識別。沒有檔案:application/x-www-form-urlencoded有檔案:multipart/form-dataASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];[request setPostValue:@"Ben" forKey:@"first_name"];[request setPostValue:@"Copsey" forKey:@"last_name"];[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];

 

下載:

1.建立請求隊列:

首先,建立網路請求隊列,如下:ASINetworkQueue   *que = [[ASINetworkQueue alloc] init];self.netWorkQueue = que;[que release];[self.netWorkQueue reset];[self.netWorkQueue setShowAccurateProgress:YES];[self.netWorkQueue go];

2.設定存放路徑

//初始化Documents路徑NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];//初始化臨時檔案路徑NSString *folderPath = [path stringByAppendingPathComponent:@"temp"];//建立檔案管理工具NSFileManager *fileManager = [NSFileManager defaultManager];//判斷temp檔案夾是否存在BOOL fileExists = [fileManager fileExistsAtPath:folderPath];if (!fileExists) {//如果不存在說建立,因為下載時,不會自動建立檔案夾[fileManager createDirectoryAtPath:folderPath            withIntermediateDirectories:YES                             attributes:nil                                 error:nil];

3.發送請求

這裡對下面幾個對象說明一下:CustomCell是我自訂的cell,cell上面有下載和暫停兩個按鈕,其tag值為cell所在的行,因此這裡的[sendertag]為下載按鈕的tag值,self.downloadArray為array數組對象,存放要下載的資源字典資訊,在該字典中有一鍵為URL,它對應的值就是我們下載連結。這些東西,根據自己的實際需要改動一下即可使用CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];NSString  *filePath = [[self.downloadArray objectAtIndex:[sender tag]] objectForKey:@"URL"];NSLog(@"filePath=%@",filePath);//初始下載路徑NSURL *url = [NSURL URLWithString:filePath];//設定下載路徑ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];//設定ASIHTTPRequest代理request.delegate = self;//初始化儲存ZIP檔案路徑NSString *savePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"book_%d.zip",[sender tag]]];//初始化臨時檔案路徑NSString *tempPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"temp/book_%d.zip.temp",[sender tag]]];//設定檔案儲存路徑[request setDownloadDestinationPath:savePath];//設定臨時檔案路徑[request setTemporaryFileDownloadPath:tempPath];//設定進度條的代理,[request setDownloadProgressDelegate:cell];//設定是是否支援斷點下載[request setAllowResumeForFileDownloads:YES];//設定基本資料[request setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:[sender tag]],@"bookID",nil]];NSLog(@"UserInfo=%@",request.userInfo);//添加到ASINetworkQueue隊列去下載[self.netWorkQueue addOperation:request];//收回request[request release];

 

/ASIHTTPRequestDelegate,下載之前擷取資訊的方法,主要擷取下載內容的大小,可以顯示下載進度多少位元組- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders { NSLog(@"didReceiveResponseHeaders-%@",[responseHeaders valueForKey:@"Content-Length"]);     NSLog(@"contentlength=%f",request.contentLength/1024.0/1024.0);    int bookid = [[request.userInfo objectForKey:@"bookID"] intValue];    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];    float tempConLen = [[userDefaults objectForKey:[NSString stringWithFormat:@"book_%d_contentLength",bookid]] floatValue];    NSLog(@"tempConLen=%f",tempConLen);    //如果沒有儲存,則持久化他的內容大小    if (tempConLen == 0 ) {//如果沒有儲存,則持久化他的內容大小        [userDefaults setObject:[NSNumber numberWithFloat:request.contentLength/1024.0/1024.0] forKey:[NSString stringWithFormat:@"book_%d_contentLength",bookid]];    } }

 

//ASIHTTPRequestDelegate,下載完成時,執行的方法- (void)requestFinished:(ASIHTTPRequest *)request {int bookid = [[request.userInfo objectForKey:@"bookID"] intValue];CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:bookid inSection:0]];cell.downloadCompleteStatus = YES;cell.progressView.progress = 0.0; }風繼續吹0連結:https://www.jianshu.com/p/c24cad69f89c來源:簡書著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。

AF2.0

1.實現請求的方法

 

2. 實現NSURLConnection (iOS 6 & 7)AFURLConnectionOperation – 它繼承於 NSOperation,負責管理 NSURLConnection,實現它的 delegate 方法。AFHTTPRequestOperation – 它繼承於 AFURLConnectionOperation,專門用於建立 HTTP 要求。2.0 的主要區別就是可以直接使用它,而不用再繼承它,原因將會在下面的 Serialization 處解釋。AFHTTPRequestOperationManager – 封裝 HTTP 要求的常見方式,GET / POST / PUT / DELETE / PATCH……
NSURLSession (iOS 7)AFURLSessionManager – 建立和管理 NSURLSession 對象,以及此對象的資料和下載/上傳等任務,實現對象和任務的代理方法。NSURLSession 的 API 本身有一些局限,AFURLSessionManager 能使其變得更好用。AFHTTPSessionManager – 它繼承於 AFURLSessionManager,封裝了 HTTP 要求的常見方式,GET / POST / PUT / DELETE / PATCH……總的來說:為了支援最新的 NSURLSession 介面,同時相容舊的 NSURLConnection,2.0 的核心組件將“網路請求”和“任務處理”分離。 AFHTTPRequestOperationManager 和 AFHTTPSessionManager 提供相似的功能,切換很方便,所以從 iOS 6 移植到 iOS 7 會很容易。之前綁在 AFHTTPClient 裡的 serialization、security 和 reachability 模型都被分離了出來,基於 NSURLSession 和 NSURLConnection 的 API 都可複用這些模型。

 

2.AFURLRequestserializtion(序列化)

概述:

RequestSerilization 是AFNetwroking中對網路請求中request這個機率的封裝。它的原型其實是NSURLRequest,將NSURLRequest進行第二次封裝,將許多諸如要求標頭,請求參數格式化, multipar/form data檔案上傳等進行了簡化處理。總結來說,使用AFURLRequestSerializer有以下幾個優點:1?自動處理的請求參數轉義,以及對不同請求方式自動對請求參數進行格式化。2?實現了multipart/form-data方式的請求。3?自動處理了User-Agent,Language等要求標頭。

使用:

AFURLRequestSerializtion在AF架構中是封裝請求這一部分對象的,作為AFHTTPSessionManaager的一個屬性被使用。如: ///    request data parsemanager.requestSerializer = [AFHTTPRequestSerializer serializer];manager.requestSerializer.timeoutInterval = 30.f;如果上傳時使用的是json格式資料,那麼使用AFJSONRequestSerializer: manager.requestSerializer = [AFJSONRequestSerializer serializer];原來存在於NSURLRequest對象的屬性,都可以該對象使用如:[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"accept"];

 

序列化(Serialization)2.0 架構的一個突破就是,請求和解析的可序列化。序列化的靈活性允許在網路層添加更多的商業邏輯,自訂更方便。<AFURLRequestSerializer> 和 <AFURLResponseSerializer> 這兩個協議,讓你在 1.0 中的一些抱怨不複存在。

 

3.安全

安全AFNetworking 支援 SSL pinning。這對涉及敏感性資料的 App 很重要。AFSecurityPolicy – 這個類通過特定認證和公用密鑰評估連結的安全性和可信任度。在你的 App bundle 中添加伺服器憑證有助於防止“中間人攻擊”。

4.

可達性(Reachability)另一個從 AFHTTPClient 中分離的功能是網路的可達性。現在你可以單獨使用它,或者作為 AFHTTPRequestOperationManager / AFHTTPSessionManager 的一個屬性。AFNetworkReachabilityManager – 負責監控當前的網路可達性,當網路的可達性發生改變時,提供相應的 callback 和通知。
UIKit 擴充2.0 的中所有 UIKit 擴充都被分離出來並進行了增強。AFNetworkActivityIndicatorManager: 新增自動開始或結束狀態列上的網路指標。UIImageView+AFNetworking: 新增顯示圖片前剪裁或者加濾鏡的功能。UIButton+AFNetworking (新增): 類似 UIImageView+AFNetworking,按鈕的背景圖從線上下載。UIActivityIndicatorView+AFNetworking (新增): 根據網路請求的狀態自動開始或結束。UIProgressView+AFNetworking (新增): 自動跟蹤某個請求的上傳下載的進度。UIWebView+AFNetworking (新增): 支援網路請求的進度和內容轉換。

5.整合

在 CocoaPods 中使用 AFNetworking 2.0:platform :ios, ‘7.0‘pod "AFNetworking", "2.0.0"

 

6.代碼

AFHTTPRequestOperation *request = [[AFHTTPRequestOperation alloc] initWithRequest:urlrequest];request.responseSerializer = [AFJSONResponseSerializer serializer];//設定回調的queue,預設是在mainQueue執行block回調request.completionQueue = your_request_operation_completion_queue();[request setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {         //設定了‘completionQueue‘後,就可以在這裡處理複雜的邏輯         //不用擔心block住了主線程    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; [request start];

 

IOS 開發 網路詳解(二)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.