AFNetworking 3.0.4 的使用

AFNetworking

 

本文永久链接:http://www.cnblogs.com/qianLL/p/5342593.html

 

pod 'AFNetworking', '~>3.0.4'    <-------第三方

具体他的pod的过过程

http://www.cnblogs.com/qianLL/p/5331624.html

代码如下 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#import "ViewController.h"
#import "AFNetworking.h"
@interface  ViewController ()
 
@end
 
@implementation  ViewController
 
- ( void )viewDidLoad {
     [ super  viewDidLoad];
     [ self  Upload];
//    [self dataTask];
//    [self MultiUpload];
//    [self Serialization];
//    [self PostMethod];
//    [self Reacheab];
     
     
}
//下载
-( void )Download{
     NSURLSessionConfiguration  *configuration=[ NSURLSessionConfiguration  defaultSessionConfiguration];
     
     AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
     
     NSURL  *URL=[ NSURL  URLWithString:@ "example/download" ];
     NSURLRequest  *request=[ NSURLRequest  requestWithURL:URL];
     
     NSURLSessionDownloadTask  *downloadTask=[manager downloadTaskWithRequest:request progress: nil  destination:^ NSURL  *( NSURL  *targetPath,  NSURLResponse  *response) {
         NSURL  *documentsDirectoryURL=[[ NSFileManager  defaultManager] URLForDirectory: NSDocumentDirectory  inDomain: NSUserDomainMask  appropriateForURL: nil  create: NO  error: nil ];
         return  [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
     } completionHandler:^( NSURLResponse  * _Nonnull response,  NSURL  * _Nullable filePath,  NSError  * _Nullable error) {
         NSLog (@ "file downloaded to :%@" ,filePath);
     }];
     [downloadTask resume];
     
}
// 上传
-( void )Upload{
     NSURLSessionConfiguration  *configuration=[ NSURLSessionConfiguration  defaultSessionConfiguration];
     
     AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
     
     NSURL  *url=[ NSURL   URLWithString:@ "example/upload.php" ];
     
     NSURLRequest  *request=[ NSURLRequest  requestWithURL:url];
     
     NSURL  *filePath=[ NSURL  fileURLWithPath:@ "path/aa.txt" ];
     
     NSURLSessionUploadTask  *uploadTask=[manager uploadTaskWithRequest:request fromFile:filePath progress: nil  completionHandler:^( NSURLResponse  * _Nonnull response,  id   _Nullable responseObject,  NSError  * _Nullable error) {
         if  (error) {
             NSLog (@ "Errof:%@" ,error);
         } else {
             NSLog (@ "Success:%@ %@" ,response,responseObject);
         }
     }];
     [uploadTask resume];
}
 
-( void )MultiUpload{
 
     NSMutableURLRequest  *request=[[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@ "POST"  URLString:@ "https:example/upload.php"   parameters: nil  constructingBodyWithBlock:^( id <AFMultipartFormData>  _Nonnull formData) {
         [formData appendPartWithFileURL:[ NSURL  fileURLWithPath:@ "path/1.png" ] name:@ "file"  fileName:@ "filename.jpg"  mimeType:@ "image/jpeg"  error: nil ];
     } error: nil ];
     
     AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:[ NSURLSessionConfiguration  defaultSessionConfiguration]];
     
     NSURLSessionUploadTask  *uploadTask;
     
     uploadTask=[manager uploadTaskWithStreamedRequest:request progress:^( NSProgress  * _Nonnull uploadProgress) {
         dispatch_async(dispatch_get_main_queue(), ^{
             [[UIProgressView  new ] setProgress:uploadProgress.fractionCompleted];
         });
     } completionHandler:^( NSURLResponse  * _Nonnull response,  id   _Nullable responseObject,  NSError  * _Nullable error) {
         if  (error) {
             NSLog (@ "errof:%@" ,error);
         } else {
             NSLog (@ "%@ %@" ,response,responseObject);
         }
     }];
     
     [uploadTask resume];
}
// data Task
-( void )dataTask{
     NSURLSessionConfiguration  *configuration=[ NSURLSessionConfiguration  defaultSessionConfiguration];
     
     AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
     
     NSURL  *url=[ NSURL  URLWithString:@ "http://1.studyios.sinaapp.com/gyxy.php?a=qq" ];
     
     NSURLRequest  *request=[ NSURLRequest  requestWithURL:url];
     
     NSURLSessionDataTask  *dataTask=[manager dataTaskWithRequest:request completionHandler:^( NSURLResponse  * _Nonnull response,  id   _Nullable responseObject,  NSError  * _Nullable error) {
         if  (error) {
             NSLog (@ "Error: %@" ,error);
         } else {
             NSLog (@ "%@ %@" ,response,responseObject);
         }
     }];
     
     [dataTask resume];
}
//GET方法
 
-( void )Serialization{
     NSURLSessionConfiguration  *configuration=[ NSURLSessionConfiguration  defaultSessionConfiguration];
     
     AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
     NSString  *url=@ "http://1.studyios.sinaapp.com/gyxy.php" ;
     NSDictionary  *parameters=@{@ "a" :@ "BB" ,@ "b" :@ "CC" ,@ "c" :@ "aa" };
    NSMutableURLRequest  *request= [[AFHTTPRequestSerializer serializer]requestWithMethod:@ "GET"  URLString:url parameters:parameters error: nil ];
     
     NSURLSessionDataTask  *dataTask=[manager dataTaskWithRequest:request completionHandler:^( NSURLResponse  * _Nonnull response,  id   _Nullable responseObject,  NSError  * _Nullable error) {
         if  (error) {
             NSLog (@ "Error: %@" ,error);
         } else {
             NSLog (@ "%@" ,responseObject);
         }
     }];
     [dataTask resume];
     
}
//POST
-( void )PostMethod{
     NSURLSessionConfiguration  *configuration=[ NSURLSessionConfiguration  defaultSessionConfiguration];
     AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
     NSString  *url=@ "http://1.studyios.sinaapp.com/mypost.php" ;
     NSDictionary  *dic=@{@ "can1" :@ "abc" ,@ "can2" :@ "bcd" };
     NSMutableURLRequest  *request=[[AFHTTPRequestSerializer serializer] requestWithMethod:@ "POST"  URLString:url parameters:dic error: nil ];
//
//    
 
     
     NSURLSessionDataTask  *dataTask=[manager dataTaskWithRequest:request completionHandler:^( NSURLResponse  * _Nonnull response,  id   _Nullable responseObject,  NSError  * _Nullable error) {
         if  (error) {
             NSLog (@ "Error: %@" ,error);
         } else {
//            NSLog(@"%@",responseObject);
         NSDictionary  *dic=[ NSJSONSerialization  JSONObjectWithData:responseObject options: NSJSONReadingMutableLeaves  error: nil ];
             NSLog (@ "%@" ,dic);
         }
     }];
     [dataTask resume];
 
}
 
-( void )Reacheab{
 
     [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
         NSLog (@ "reacheability:%@" ,AFStringFromNetworkReachabilityStatus(status));
     }];
     [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
-( void )SSLCertificates{
     AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
     manager.securityPolicy.allowInvalidCertificates= YES ;
}
- ( void )didReceiveMemoryWarning {
     [ super  didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
}
 
@end

 

 

转载于:https://www.cnblogs.com/wujie123/p/5343222.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值