iOS网络第三方使用方法大全AFNetworking/ASIHTTPRequest/MKNetworkKit
1. 参与对比的孩子有 AFNetworking/ASIHTTPRequest/MKNetworkKit
1. GET
1~ 新建工程: SingleProject 带故事板的、 为了我们测试的方便
2~ 打开系统命令提示符 -> 使用cocoapods 下载这3个第三方库 -> 如果不会使用cocoapods请百度cocoapods-> 然后确认环境、 进行安装
3~ podfile:
platform: iOS, ‘7.0‘ pod "AFNetworking", "~> 版本号" // 版本号自己去看 pod "ASIHTTPRequest", "~>" pod "MKNetworkkit", "~>"
4~ 开始对比方法 我们使用ARC 注:ASI是MRC 如果需要的话, 在Buile Phases里边为她的文件添加 -fno-objc-arc
1 // 2 // ADViewController.m 3 // Networking 4 // 5 // Created by 薛雨仑 on 14-10-6. 6 // Copyright (c) 2014年 Dylan. All rights reserved. 7 // 8 9 #import "ADViewController.h" 10 #import <AFNetworking.h> 11 #import <ASIHTTPRequest.h> 12 #import <MKNetworkKit.h> 13 14 @interface ADViewController () <ASIHTTPRequestDelegate> 15 16 @end 17 18 @implementation ADViewController 19 20 - (void)viewDidLoad 21 { 22 [super viewDidLoad]; 23 24 // Get 25 AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; 26 [manager GET:@"example.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 27 28 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 29 30 }]; 31 32 ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"example.com"]]; 33 request.delegate = self; 34 [request startAsynchronous];
// request setDidFinishSelector:<#(SEL)#>
35 // [request setCompletionBlock:^{ 36 // 37 // }]; 38
MKNetworkEngine * engine = [[MKNetworkEngine alloc] initWithHostName:@"127.0.0.1"];
39 MKNetworkOperation * operation = [[MKNetworkOperation alloc] initWithURLString:@"example.com" params:nil httpMethod:@"GET"];
[engine enqueueOperation:operation];
40 [operation addCompletionHandler:^(MKNetworkOperation *completedOperation) { 41 NSLog(@"%@", [completedOperation responseData]); 42 } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) { 43 NSLog(@"%@", [error localizedDescription]); 44 }]; 45 46 // POST 47 } 48 49 - (void)requestFinished:(ASIHTTPRequest *)request { 50 NSLog(@"%@", [request responseString]); 51 } 52 53 #pragma mark - ASIHttpRequestDelegateMethods 54 55 - (void)didReceiveMemoryWarning 56 { 57 [super didReceiveMemoryWarning]; 58 // Dispose of any resources that can be recreated. 59 } 60 61 @end
2.POST
1 // POST 2 NSDictionary * parameters = @{@"key": @"value"}; 3 AFHTTPRequestOperationManager * manager_post = [AFHTTPRequestOperationManager manager]; 4 [manager_post POST:@"example.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 5 6 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 7 8 }]; 9 10 // form post 11 ASIFormDataRequest * request_post = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"example.com"]]; 12 [request_post setPostValue:@"value" forKey:@"key"]; 13 [request_post startAsynchronous]; 14 [request_post setCompletionBlock:^{ 15 NSLog(@"do what"); 16 }]; 17 18 MKNetworkOperation * operation_post = [[MKNetworkOperation alloc] initWithURLString:@"example.com" params:parameters httpMethod:@"POST"];
[engine enqueueOperation:operation];
19 [operation_post addCompletionHandler:^(MKNetworkOperation *completedOperation) { 20 21 } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) { 22 23 }];
是不是发现了 MK AF 在Block的使用程度上高于ASI, MK的内聚貌似更好一点。 所以我们推荐MKNetworkKit这个新生儿。 继续往下看 我们开始主要对比AF MK
3.Download
1 // Download 2 AFURLSessionManager * download_manager_af = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 3 NSURLSessionDownloadTask * task = [download_manager_af downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"example.com"]] progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 4 NSURL * filepath = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; 5 // path 6 return [filepath URLByAppendingPathComponent:[response suggestedFilename]]; 7 } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 8 NSLog(@"%@", filePath); 9 }]; 10 [task resume]; 11 12 MKNetworkOperation * download_mk = [[MKNetworkOperation alloc] initWithURLString:@"example.com" params:nil httpMethod:@"GET"]; 13 [download_mk addDownloadStream:[NSOutputStream outputStreamToFileAtPath:@"path" append:YES]]; 14 [engine enqueueOperation:download_mk]; 15 16 [download_mk onDownloadProgressChanged:^(double progress) { 17 18 }]; 19 20 [download_mk addCompletionHandler:^(MKNetworkOperation *completedOperation) { 21 22 } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) { 23 24 }];
发现MK内聚程度是不是高于AF啊。 而且使用很上手。
4.Upload
1 // upload 2 AFURLSessionManager * upload_af = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 3 NSURLSessionTask * upload_tast = [upload_af uploadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"example.com"]] fromFile:[[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil] URLByAppendingPathComponent:@"fileName"] progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { 4 if (error) { 5 NSLog(@"%@", [error localizedDescription]); 6 } else { 7 NSLog(@"Success"); 8 } 9 }]; 10 [upload_tast resume]; 11 12 MKNetworkOperation * upload_mk = [[MKNetworkOperation alloc] initWithURLString:@"example.com" params:[NSDictionary dictionaryWithObject:@"username" forKey:@"username"] httpMethod:@"POST"]; 13 [upload_mk addFile:@"filepath" forKey:@"fikekey" mimeType:@"filetype"]; 14 [upload_mk setFreezable:YES]; 15 16 [engine enqueueOperation:upload_mk]; 17 [upload_mk onUploadProgressChanged:^(double progress) { 18 19 }]; 20 21 [upload_mk addCompletionHandler:^(MKNetworkOperation *completedOperation) { 22 23 } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) { 24 25 }];
附言: MKNetworkKit包含许多实用类目, 比如url的编码, 时间获取, 格式转换等, 直接引入就可以使用, 具体操作大家慢慢去发现。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。