寫在前面(廢話,可略過)又是好久沒更新,主要是忙(懶)。爭取多學習,多分享! 正式開始我們在開發的過程中,難免會遇到網絡操作,我們可以使用iOS原裝的網絡框架,當然了,使用三方框架更容易些,非常出名的就是AFNetworking框架,這個框架貌似還是12年最佳三方框架。總之,iOS開發,網絡部分,基本都會用到這個框架。 這個框架在github上,AFNetworking 傳送門 --> 點我點我 如果使用此框架的1.x版本,在JSON解析方面使用的某一個API,但是在2.x以后,AFNetworking推薦使用另一個API,而網上大部分教程都是1.x JSON API,此文主要分享下2.x的方法。 先讓大家感受下1.x的 //構建網址 NSString *urlString = [NSString stringWithFormat:@"xxxxxxxxxx"];//xxxx處寫一個你的網址 //如果網址中有中文,需要轉換 urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //構建NSURL NSURL *url = [NSURL URLWithString:urlString]; //構建請求,這個構建方法是基本構建方法的一個封裝加強。主要多了超時屬性,就是最后一個參數,4.0f。意思就是如果在4秒內沒有響應,就不阻塞主線程(為啥放主線程就不贅述了) NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUsePRotocolCachePolicytimeoutInterval:4.0f]; //以AF開始的是就是AFNetworking框架的API,這是1.x的方法。 AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:requestsuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSDictionary *dictionary = JSON; int ret = [[dictionary objectForKey:@"ret"] intValue]; //對服務器返回數據進行判斷 switch (ret) { case -1: [self showAlertWithString:@"用戶未登錄"]; break; case 1: [self saveDataToCurrentAccount]; [self showAlertWithDelegateWithString:@"保存成功"]; break; case -2: [self showAlertWithString:@"保存失敗"]; break; } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { [self showAlertWithString:[NSString stringWithFormat:@"%@", error.localizedDescription]]; }]; [op start]; 相信大家都能看懂, 它很好的使用了block,如果成功,todo,如果失敗,tudo。(成功、失敗里面的方法是我自己封裝的,你寫你自己的就OK) 并且整體方法放入operation。 直接把返回的JSON以參數形式給你。 這就是1.x的JSON方法,但是如果使用的是2.x的框架,再使用這個方法就會報錯,因為2.x的框架取消了AFJSONRequestOperation 這個時候我們應該怎么做呢? 讓大家感受下2.x的 NSString *urlString = [NSString stringWithFormat:@"xxxxxxxx"]; //有中文,需要轉換 urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:4.0f]; AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; op.responseSerializer = [AFJSONResponseSerializer serializer]; [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responSEObject) { NSDictionary *dictionary = responseObject; int ret = [[dictionary objectForKey:@"ret"] intValue]; //對服務器返回數據進行判斷 switch (ret) { case -1: [self showAlertWithString:@"用戶未登錄"]; break; case 1: [self saveDataToCurrentAccount]; [self showAlertWithDelegateWithString:@"保存成功"]; break; case -2: [self showAlertWithString:@"保存失敗"]; break; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [self showAlertWithString:[NSString stringWithFormat:@"%@", error.localizedDescription]]; }]; [[NSOperationQueue mainQueue] addOperation:op]; 在2.x中,他使用了AFHTTPRequestOperation和AFJSONResponseSerializer,對結果用他自己的API序列化,這時候,沒有id JSON了,但是你把responseObject當 id JSON用就OK了。 最后提提覺得大家應該都知道,但是還是寫出來吧。 如何導入框架呢? 給兩個方法吧 一。直接去傳送門找需要的下載,一般我們使用第一個,也就是默認的AFNetworking框架。進去后,看右側,有個download,下載、解壓,里面有個文件夾,找找,就是框架的.h.m,具體文件結構忘了,不過很好找的。然后把這個文件夾拖入工程,選copy,group,你的target。然后在需要使用的地方import AFNetworking.h 就OK。 二。使用cocoaPods配置 |
新聞熱點
疑難解答