AFNetworking的基本使用
網(wǎng)絡(luò)請(qǐng)求
- AFNetworking的基本用法和NSURLSession的用法基本是相同的
- AFNetworking的基本使用類
- AFHTTPSessionManager的功能
方法 |
功能 |
-(NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters progress:(void (^)(NSProgress * _Nonnull))downloadProgress success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure |
get請(qǐng)求 |
- (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure |
post請(qǐng)求 |
- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler |
下載大文件 |
- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block |
監(jiān)聽網(wǎng)絡(luò)狀態(tài) |
- 注意:如果要想刷新頁(yè)面這些需要在主線程進(jìn)行操作的任務(wù),其實(shí)就在block中進(jìn)行就可以,因?yàn)锳FNetworking已經(jīng)幫你回到了主線程
get請(qǐng)求
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
//添加返回的值的類型
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
[mgr GET:@"http://b.hiphotos.baidu.com/image/w%3D2048/sign=6be5fc5f718da9774e2f812b8469f919/8b13632762d0f703b0faaab00afa513d2697c515.jpg" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//如果要顯示進(jìn)度,就要來到這里,由于請(qǐng)求下來的數(shù)據(jù)是data,用代理方法是一段一段下來的,所以只要下來一段,就會(huì)執(zhí)行這個(gè)block,所有用當(dāng)前進(jìn)度除以總進(jìn)度就會(huì)展現(xiàn)出下載的進(jìn)度條
[self.slider setValue:1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount animated:YES];
NSLog(@"進(jìn)度 = %f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//成功就會(huì)走這里,responseObject是字典或者數(shù)組
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//失敗就會(huì)走到這里
NSLog(@"err = %@",error);
}];
post請(qǐng)求
AFHTTPSessionManager *mgr =[AFHTTPSessionManager manager];
//這里可以添加評(píng)論,把評(píng)論放到這里,放入parameters字典中返回給服務(wù)器,請(qǐng)求成功后刷新cell就會(huì)展現(xiàn)評(píng)論
NSString *content = @"你別吃了,再吃就胖死了";
NSString *str =[content stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[mgr POST:@"http://api2.pianke.me/comment/add" parameters:@{@"auth":@"XZU7RH7m1861DC8Z8H8HvkTJxQMGoPLGO9zo4XDA0cWP22NdFSh9d7fo",@"client":@1,@"content":str,@"contentid":@"553a073adfa688b70300001c",@"deviceid":@"6D4DD967-5EB2-40E2-A202-37E64F3BEA31",@"recid":@"",@"reuid":@"",@"version":@"3.0.6"}
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//上傳數(shù)據(jù)(這里只做上傳數(shù)據(jù))
} progress:^(NSProgress * _Nonnull uploadProgress) {
//進(jìn)度條
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//成功,responseObject就是返回的數(shù)據(jù)
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//失敗
NSLog(@"%@",error);
}];
下載大文件
AFHTTPSessionManager *mgr =[AFHTTPSessionManager manager];
//設(shè)置請(qǐng)求
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString: @"網(wǎng)絡(luò)請(qǐng)求" ]];
[mgr downloadTaskWithRequest:req progress:^(NSProgress * _Nonnull downloadProgress) {
//這里是進(jìn)度條
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//這里第一個(gè)參數(shù)你要給一個(gè)文件下載的路徑
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//這里是下載完成給你的響應(yīng),文件路徑,錯(cuò)誤,
}];
文件上傳
[mgr POST:@"http://api2.pianke.me/comment/add" parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//設(shè)置上傳的文件
NSData *data = [NSData dataWithContentsOfFile:@"用戶上傳的本地文件的地址"];
//這里設(shè)置上傳文件的請(qǐng)求頭,請(qǐng)求體的設(shè)置AFMultipartFormData幫你拼接好了,只要調(diào)下面的方法就可以了
[formData appendPartWithFileData:data name:<#(nonnull NSString *)#> fileName:<#(nonnull NSString *)#> mimeType:<#(nonnull NSString *)#>];
} progress:^(NSProgress * _Nonnull uploadProgress) {
//進(jìn)度條
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//上傳成功,返回響應(yīng)
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//失敗
NSLog(@"%@",error);
}];
監(jiān)聽網(wǎng)絡(luò)狀態(tài)
- 一般在AppDelegate里面監(jiān)聽的是整個(gè)app的狀態(tài),在控制器監(jiān)聽的是當(dāng)前的頁(yè)面,
- 如果監(jiān)聽的話可以寫個(gè)bool型的方法去監(jiān)聽,返回是否有網(wǎng)絡(luò)
//這里去監(jiān)聽是否有網(wǎng)
+ (BOOL)isNet
{
return [AFNetworkReachabilityManager manager].reachableViaWiFi ||[AFNetworkReachabilityManager manager].reachableViaWWAN;
}
//檢測(cè)網(wǎng)絡(luò)
AFNetworkReachabilityManager *magr = [ AFNetworkReachabilityManager sharedManager];
[magr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"不清楚");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"連接不上");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"蜂窩網(wǎng)");
break;
}
}];
//開啟檢測(cè)
[magr startMonitoring];