目前使用人数最多的第三方网络库,没有之一。从开始的NSURLConnection到现在的NSURLSession,它都一直保持着与苹果的步调一致,而由它也衍生出大量的相关第三方网络功能库,不仅仅因为他的可靠,好用,一直保持着维护更新,也是为什么它这么受到广大程序员的青睐。
接下来我们来看几个demo
1 // 2 // ViewController.m 3 // AFNetworking3.0简介 4 // 5 6 7 #import "ViewController.h" 8 #import "AFNetworking.h" 9 @interface ViewController () 10 11 @end 12 13 @implementation ViewController 14 15 - (void)viewDidLoad { 16 [super viewDidLoad]; 17 18 [self upLoadData]; 19 20 } 21 //上传data 22 -(void)upLoadData{ 23 24 UIImage * image = [UIImage imageNamed:@"renminbi.jpg"]; 25 26 NSData * data = UIImageJPEGRepresentation(image, 1); 27 28 AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; 29 /* 30 POST 上传地址 31 parameters 文本参数 32 constructingBodyWithBlock 上传文件的block,有可能多次调用 33 progress 上传进度 34 success 上传成功 35 failure 上传失败 36 */ 37 [manager POST:@"http://localhost/post/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 38 39 /* 40 41 formData 上传的数据 42 43 FileData 上传文件的data 44 45 name 上传文件的key 46 47 fileName 服务器上的名字 48 49 mimeType 上传资源的类型 50 51 */ 52 53 [formData appendPartWithFileData:data name:@"userfile00" fileName:@"xubaoaichiyu" mimeType:@"image/jpg"]; 54 55 } progress:^(NSProgress * _Nonnull uploadProgress) { 56 57 NSLog(@"%@",uploadProgress); 58 59 60 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 61 62 NSLog(@"成功->%@",responseObject); 63 64 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 65 66 NSLog(@"失败->%@",error); 67 68 }]; 69 70 } 71 72 @end
1 //上传文件 2 AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; 3 4 /* 5 POST 上传地址 6 parameters 文本参数 7 constructingBodyWithBlock 上传文件的block,有可能多次调用 8 progress 上传进度 9 success 上传成功 10 failure 上传失败 11 */ 12 [manager POST:@"http://localhost/post/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 13 14 15 16 /* 17 18 formData 上传的数据 19 20 FileData 上传文件的data 21 22 name 上传文件的key 23 24 fileName 服务器上的名字 25 26 mimeType 上传资源的类型 27 28 */ 29 30 NSURL * url = [[NSBundle mainBundle] URLForResource:@"renminbi.jpg" withExtension:nil]; 31 32 [formData appendPartWithFileURL:url name:@"userfile00" fileName:@"xubaoaichiyu" mimeType:@"image/jpg" error:nil]; 33 34 } progress:^(NSProgress * _Nonnull uploadProgress) { 35 36 NSLog(@"%@",uploadProgress); 37 38 39 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 40 41 NSLog(@"成功->%@",responseObject); 42 43 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 44 45 NSLog(@"失败->%@",error); 46 47 }];
1 AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; 2 3 [manager GET:@"http://localhost/post/upload.php" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { 4 5 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 6 7 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 8 9 }];
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; [manager POST:@"http://localhost/post/upload.php" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
1 AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; 2 3 manager.requestSerializer = [AFHTTPRequestSerializer serializer]; 4 5 manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; 6 7 [manager GET:@"http://localhost/post/upload.php" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { 8 9 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 10 11 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 12 13 }];