Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

iOS开发之AFNetworking 3.0.4使用-OC

 

 

GET请求

 1 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
 2 
 3 [manager GET:URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {  
 4 
 5 }     
 6  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {  
 7 
 8  NSLog(@"这里打印请求成功要做的事");  
 9 
10 }
11 
12 failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {  
13 
14 NSLog(@"%@",error);  //这里打印错误信息
15 
16 }];

 

POST 请求

 

 1 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
 2 
 3 
 4 NSMutableDictionary *parameters = @{@"":@"",@"":@""};
 5 
 6 [manager POST:URL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
 7 
 8 
 9 } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
10 
11 
12 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
13 
14 }];

 

暂时用的比较多的就这两个,用到其他的 再来更新!!

最近又用到了下载,索性 上传下载都研究了一下 ,补充进来

补充试用AFN3.0下载方法

 1 /**
 2  *  AFN3.0 下载
 3  */
 4 - (void)downLoad{
 5 
 6     //1.创建管理者对象
 7     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
 8     //2.确定请求的URL地址
 9     NSURL *url = [NSURL URLWithString:@""];
10 
11     //3.创建请求对象
12     NSURLRequest *request = [NSURLRequest requestWithURL:url];
13 
14     //下载任务
15     NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
16         //打印下下载进度
17         WKNSLog(@"%lf",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
18 
19     } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
20         //下载地址
21         WKNSLog(@"默认下载地址:%@",targetPath);
22 
23         //设置下载路径,通过沙盒获取缓存地址,最后返回NSURL对象
24         NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
25         return [NSURL URLWithString:filePath];
26 
27 
28     } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
29 
30         //下载完成调用的方法
31         WKNSLog(@"下载完成:");
32         WKNSLog(@"%@--%@",response,filePath);
33 
34     }];
35 
36     //开始启动任务
37     [task resume];
38 
39 }

 

AFN3.0 上传

 1 /**
 2  *  AFN 3.0 上传
 3  *      有两种方式
 4  *          upLoad1 和 upLoad2
 5  */
 6 
 7 //第一种方法是通过工程中的文件进行上传
 8 - (void)upLoad1{
 9 
10     //1。创建管理者对象
11     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
12 
13     //2.上传文件
14     NSDictionary *dict = @{@"username":@"1234"};
15 
16     NSString *urlString = @"22222";
17     [manager POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
18         //上传文件参数
19         UIImage *iamge = [UIImage imageNamed:@"123.png"];
20         NSData *data = UIImagePNGRepresentation(iamge);
21         //这个就是参数
22         [formData appendPartWithFileData:data name:@"file" fileName:@"123.png" mimeType:@"image/png"];
23 
24     } progress:^(NSProgress * _Nonnull uploadProgress) {
25 
26         //打印下上传进度
27         WKNSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
28     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
29 
30         //请求成功
31         WKNSLog(@"请求成功:%@",responseObject);
32 
33     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
34 
35         //请求失败
36         WKNSLog(@"请求失败:%@",error);
37     }];
38 
39 }
40 
41 //第二种是通过URL来获取路径,进入沙盒或者系统相册等等
42 - (void)upLoda2{
43     //1.创建管理者对象
44     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
45     //2.上传文件
46     NSDictionary *dict = @{@"username":@"1234"};
47 
48     NSString *urlString = @"22222";
49     [manager POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
50 
51         [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"文件地址"] name:@"file" fileName:@"1234.png" mimeType:@"application/octet-stream" error:nil];
52     } progress:^(NSProgress * _Nonnull uploadProgress) {
53 
54         //打印下上传进度
55         WKNSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
56     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
57 
58         //请求成功
59         WKNSLog(@"请求成功:%@",responseObject);
60 
61     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
62 
63         //请求失败
64         WKNSLog(@"请求失败:%@",error);
65     }];
66 }

 

监测当前网络状态(网络监听)

 1 - (void)AFNetworkStatus{
 2 
 3     //1.创建网络监测者
 4     AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
 5 
 6     /*枚举里面四个状态  分别对应 未知 无网络 数据 WiFi
 7      typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
 8          AFNetworkReachabilityStatusUnknown          = -1,      未知
 9          AFNetworkReachabilityStatusNotReachable     = 0,       无网络
10          AFNetworkReachabilityStatusReachableViaWWAN = 1,       蜂窝数据网络
11          AFNetworkReachabilityStatusReachableViaWiFi = 2,       WiFi
12      };
13      */
14 
15     [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
16         //这里是监测到网络改变的block  可以写成switch方便
17         //在里面可以随便写事件
18         switch (status) {
19             case AFNetworkReachabilityStatusUnknown:
20                 WKNSLog(@"未知网络状态");
21                 break;
22             case AFNetworkReachabilityStatusNotReachable:
23                 WKNSLog(@"无网络");
24                 break;
25 
26             case AFNetworkReachabilityStatusReachableViaWWAN:
27                 WKNSLog(@"蜂窝数据网");
28                 break;
29 
30             case AFNetworkReachabilityStatusReachableViaWiFi:
31                 WKNSLog(@"WiFi网络");
32 
33                 break;
34 
35             default:
36                 break;
37         }
38 
39     }] ;
40 }

 

 

 

 

 

posted on 2016-07-28 10:06  Sinner_Yun  阅读(417)  评论(0编辑  收藏  举报

导航