AFNetworking開源庫封裝了原生的方法,
iOS9.0之后参淹,
由于NSURLConnection的棄用宁炫,
AFNetworking的使用也有一些改變。
GET請求:
//GET請求
-(void)demo1{
NSString *urlString = @"http://www.liubaiqi.cn";
AFHTTPSessionManager *manger =[AFHTTPSessionManager manager];
[manger GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
POST請求:
//POST請求
-(void)demo2{
NSString *urlString = @"http://192.168.1.101:8080";
AFHTTPSessionManager *manger =[AFHTTPSessionManager manager];
NSMutableDictionary *parameter= @{@"":@"",@"":@""};
[manger POST:urlString parameters:parameter progress:^(NSProgress * _NonnulluploadProgress){
}success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
Download請求:
//DownLoad請求
-(void)demo3{
//1. 創(chuàng)建NSURLSessionConfiguration
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//2. 創(chuàng)建管理者對象
AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
//3. 設置url
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/1.mp4"];
//4. 創(chuàng)建請求對象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//5. 下載任務
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//打印下載進度
NSLog(@"%lf",1.0*downloadProgress.completedUnitCount/downloadProgress.totalUnitCount);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull 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];
}
Upload請求:
//UpLoad請求
-(void)demo4{
//創(chuàng)建NSURLSessionConfiguration
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//創(chuàng)建管理者對象
AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
//設置url
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1"];
//創(chuàng)建請求對象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//文件路徑
NSURL *filePath = [NSURL fileURLWithPath:@"file://Users/Liu/Desktop/Note"];
//上傳任務
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if(error){
//錯誤
NSLog(@"Error:%@",error);
}else{
//成功
NSLog(@"Success %@ %@",response,responseObject);
}
}];
//啟動任務
[uploadTask resume];
}
網(wǎng)絡狀態(tài):
//網(wǎng)絡狀態(tài)
-(void)demo5{
//1. 創(chuàng)建網(wǎng)絡監(jiān)測者
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//監(jiān)測網(wǎng)絡改變
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知網(wǎng)絡狀態(tài)");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"無網(wǎng)絡");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"蜂窩數(shù)據(jù)網(wǎng)絡");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi網(wǎng)絡");
break;
default:
break;
}
}];
}
ps:本文參考組長的博客:MrFung's Blog