AFNetworking是一個 在iOS開發中 使用很是多網絡開源庫.它是一個輕量級的網絡請求API類庫.javascript
適用於iOS以及Mac OS X。它構建於在(Apple iOS開發文檔) NSURLConnection , NSOperation , 以及其餘熟悉的Foundation技術之上。php
下面的類已從AFNetworking 3.0中廢棄:html
如今核心代碼:AFHTTPSessionManager,<AFNrtworking3.1.0目前使用NSURLSession做爲網絡類!!!!>java
它擁有良好的架構,豐富的api,以及模塊化構建方式,使得使用起來很是輕鬆。json
官方連接http://cocoadocs.org/docsets/AFNetworking/1.3.0/api
重要功能梳理:網絡
下面咱們就來看看AFNetworking的基本使用:session
1 #pragma mark - 網絡監測按鈕的響應方法 2 - (IBAction)networkingMonitoringAction:(id)sender { 3 //isOpen是本身定義的一個Bool值的屬性,來切換開關網絡檢測這一功能. 4 if (!self.isOpen) { 5 //打開網絡監測 6 [[AFNetworkReachabilityManager sharedManager] startMonitoring]; 7 self.isOpen = YES; 8 } else { 9 //關閉網絡監測 10 [[AFNetworkReachabilityManager sharedManager] stopMonitoring]; 11 self.isOpen = NO; 12 } 13 //接下來判斷當前網絡狀態:wifi,2G/3G/4G,無網絡訪問 14 [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 15 switch (status) { 16 case AFNetworkReachabilityStatusUnknown: 17 NSLog(@"當前網絡未知"); 18 break; 19 case AFNetworkReachabilityStatusNotReachable: 20 NSLog(@"當前無網絡訪問"); 21 break; 22 case AFNetworkReachabilityStatusReachableViaWWAN: 23 NSLog(@"當前手機流量網絡"); 24 break; 25 case AFNetworkReachabilityStatusReachableViaWiFi: 26 NSLog(@"當前wifi鏈接網絡"); 27 break; 28 29 default: 30 break; 31 } 32 }]; 33 }
//初始化session對象
self.session = [AFHTTPSessionManager manager];
//設置請求接口回來的時候支持什麼類型的數據
// self.session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", nil]; (以前忘了,如今添加上)架構
這一步必不可少,必須寫明要獲取數據的類型,否則會報錯.app
下面咱們來看看怎麼實現數據請求:
1 #pragma mark - GET請求 2 - (IBAction)getRequestAction:(id)sender { 3 4 //參數一:get請求的網址 5 //參數二:拼接參數body 6 //參數三:progress表示進度的意思 7 //參數四:數據請求成功後執行的操做 8 //參數五:請求失敗後報的錯誤 9 [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { 10 NSLog(@"下載的進度"); 11 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 12 NSLog(@"請求成功%@", responseObject); 13 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 14 NSLog(@"請求失敗%@", error); 15 }]; 16 } 17 18 #pragma mark - post請求 19 - (IBAction)postRequestAction:(id)sender { 20 /* 21 body: { 22 do = "pri_memberlist"; 23 "member_id" = zpHr2dsRvQQxYJxo2; 24 "workspace_id" = ILfYpE4Dhs2gWcuQx; 25 }*/ 26 NSString *url = @"http://m.taskwedo.com/API/wedo1/wedo.php"; 27 28 //通常的body都是給你一個字典類型的,須要你建立一個字典來接受存儲 29 NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 30 [dic setValue:@"pri_memberlist" forKey:@"do"]; 31 [dic setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"]; 32 [dic setValue:@"ILfYpE4Dhs2gWcuQx" forKey:@"workspace_id"]; 33 34 [self.session POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) { 35 NSLog(@"上傳進度"); 36 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 37 NSLog(@"請求成功%@", responseObject); 38 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 39 NSLog(@"失敗啦,哈哈%@", error); 40 }]; 41 42 43 } 44 45 #pragma mark - post請求2(當body中含有漢字或特殊字符的時候,這時候須要轉碼) 46 - (IBAction)postTwoRequestAction:(id)sender { 47 /* BODY: address = ""; 48 comment = "\U7c7b\U6a21\U5757\U8ba1\U5212\U7528\U5230\U7b2c\U4e09\U90e8\U5206\U4e2d\Uff0c\U5f85\U63d0\U95ee\U3001\U56de\U7b54\U79ef\U7d2f\U5230\U4e00\U5b9a\U6570\U91cf\U65f6\Uff0c\U4fbf\U4e8e\U5927\U5bb6\U7684\U95ee\U9898\U7684\U5feb\U901f\U67e5\U627e\Uff0c\U6240\U4ee5\U63d0\U95ee\U90e8\U5206\U6682\U65f6\U4e0d\U52a0\U5165\U8fd9\U4e2a"; 49 do = "add_comment"; 50 kind = task; 51 "member_id" = zpHr2dsRvQQxYJxo2; 52 other = ""; 53 "task_id" = 55a47e79ec25e3641;*/ 54 55 NSString *urlStr = @"http://m.taskwedo.com/API/wedo1/wedo.php"; 56 57 NSString *comment = @"類模塊計劃用到第三部分中,待提問、回答積累到必定數量時,便於你們的問題的快速查找,因此提問部分暫時不加入這個"; 58 //對漢字進行轉碼操做 59 comment = [comment stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; 60 61 NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 62 [dic setValue:@"" forKey:@"address"]; 63 [dic setValue:comment forKey:@"comment"]; 64 [dic setValue:@"add_comment" forKey:@"do"]; 65 [dic setValue:@"task" forKey:@"kind"]; 66 [dic setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"]; 67 [dic setValue:@"" forKey:@"other"]; 68 [dic setValue:@"55a47e79ec25e3641" forKey:@"task_id"]; 69 70 //請求報文 71 [self.session POST:urlStr parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) { 72 NSLog(@"上傳進程%@", uploadProgress); 73 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 74 NSLog(@"請求成功%@", responseObject); 75 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 76 NSLog(@"請求失敗%@", error); 77 }]; 78 79 //能夠看到,在第三方庫中,咱們直接就能夠調用GET和POST請求方法,不用像之前同樣,寫那麼多行代碼了,大大簡化了代碼量 80 }
注意:AFNetworking能夠進行JSON數據解析/Plist數據解析.(不支持XML數據解析)URL字符串中若是有特殊字符或者中文字符,AFNETWorking並無作UTF8的轉碼,須要:url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];