1、AFNetworking簡介javascript
2、AFNetworking功能梳理php
注意:AFNetworking能夠進行JSON數據解析/Plist數據解析.(不支持XML數據解析)。URL字符串中若是有特殊字符或者中文字符,AFNETWorking並無作UTF8的轉碼,須要:url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];html
3、代碼示例java
用CocoaPods導入AFNetworking(用storyboard建立按鈕,並關聯事件):json
#import "ViewController.h" // 網絡請求的頭文件 #import <AFNetworking.h> #import <AFNetworkActivityIndicatorManager.h> @interface ViewController () { /// 進行網絡監測判斷的Bool值 BOOL isOpen; } /// 用於網絡請求的session對象 @property (nonatomic, strong) AFHTTPSessionManager *session; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化session對象 self.session = [AFHTTPSessionManager manager]; // 設置請求接口回來的時候支持什麼類型的數據 self.session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html",nil]; } #pragma makr - 網絡監測按鈕的響應方法 - (IBAction)networkMonitoringAction:(id)sender { if (!isOpen) { // 打開網絡監測 [[AFNetworkReachabilityManager sharedManager] startMonitoring]; isOpen = YES; }else { // 關閉網絡監測 [[AFNetworkReachabilityManager sharedManager] stopMonitoring]; isOpen = NO; } // 接下來會判斷當前是wifi狀態仍是3G狀態仍是網絡不可用狀態 [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusUnknown: NSLog(@"當前網絡處於未知狀態"); break; case AFNetworkReachabilityStatusNotReachable: NSLog(@"當前網絡處於未鏈接狀態"); break; case AFNetworkReachabilityStatusReachableViaWWAN: NSLog(@"3G或4G網絡"); break; case AFNetworkReachabilityStatusReachableViaWiFi: NSLog(@"WiFi"); break; default: break; } }]; } #pragma mark - get請求 - (IBAction)getRequestAction:(id)sender { [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { NSLog(@"這事進度條 = %@", downloadProgress); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"請求成功:%@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"請求失敗%@", error); }]; } #pragma mark - post請求 - (IBAction)postRequestAction:(id)sender { /*{ do = "pri_memberlist"; "member_id" = zpHr2dsRvQQxYJxo2; "workspace_id" = ILfYpE4Dhs2gWcuQx; }*/ [AFNetworkActivityIndicatorManager sharedManager].enabled = YES; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; NSString *urlStr = @"http://m.taskwedo.com/API/wedo1/wedo.php"; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setValue:@"pri_memberlist" forKey:@"do"]; [dict setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"]; [dict setValue:@"ILfYpE4Dhs2gWcuQx" forKey:@"workspace_id"]; [self.session POST:urlStr parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) { NSLog(@"上傳進度 = %@", uploadProgress); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"post請求成功 = %@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"請求失敗 = %@", error); }]; } #pragma mark post請求 - (IBAction)post2RequestAction:(id)sender { /*address = ""; 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"; do = "add_comment"; kind = task; "member_id" = zpHr2dsRvQQxYJxo2; other = ""; "task_id" = 55a47e79ec25e3641;*/ NSString *urlStr = @"http://m.taskwedo.com/API/wedo1/wedo.php"; NSString *commonContent = @"類模塊計劃用到第三部分中,待提問、回答積累到必定數量時,便於你們的問題的快速查找,因此提問部分暫時不加入這個"; commonContent = [commonContent stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setValue:@"" forKey:@"address"]; [dic setValue:commonContent forKey:@"comment"]; [dic setValue:@"add_comment" forKey:@"do"]; [dic setValue:@"task" forKey:@"kind"]; [dic setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"]; [dic setValue:@"" forKey:@"other"]; [dic setValue:@"55a47e79ec25e3641" forKey:@"task_id"]; [self.session POST:urlStr parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) { NSLog(@"上傳進度 = %@", uploadProgress); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"post請求成功 = %@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"請求失敗 = %@", error); }]; } @end