一、网络请求:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject : %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error : %@", error.description);
}];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:URLString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject : %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error : %@", error);
}];
二、文件上传:
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
[sessionManager POST:URL parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"a.jpg" ofType:nil];
NSData *imgData = [NSData dataWithContentsOfFile:filePath];
[formData appendPartWithFileData:imgData name:@"pic" fileName:@"filename" mimeType:@"image/jpg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
// 获取上传的进度
NSLog(@"%.2f",uploadProgress.fractionCompleted);
NSLog(@"线程:%@",[NSThread currentThread]); // 子线程
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 请求成功
NSLog(@"请求成功:%@",responseObject);
NSLog(@"线程:%@",[NSThread currentThread]); // 主线程
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 请求失败
NSLog(@"请求失败:%@",error);
}];
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
[sessionManager POST:@" " parameters:@{@" " : @" "} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在block中设置需要上传的文件
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"文件路径"] name:@"file" error:nil];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"成功:%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"失败:%@", error);
}];
三、文件下载
- 使用 NSURLSessionDownloadTask 类
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURL *url = [NSURL URLWithString:@" "];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 下载文件
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
// block会实时调用
NSLog(@"%.2f",downloadProgress.fractionCompleted);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
// NSHomeDirectory()可以得到应用程序目录的路径
// 返回一个URL存储文件
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/***.mp3"];
NSURL *url = [NSURL fileURLWithPath:filePath];
return url;
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (error) {
NSLog(@"下载失败 ~~~ %@",error);
}else {
NSLog(@"下载成功");
}
}];
// 开始下载任务
[task resume];
四、网络监听
- 使用 AFNetworkReachabilityManager 类
// 获得网络监控的管理者
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager manager];
// 只要网络环境发生变化,就会调用此 block
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
/* 枚举里面的四个状态
AFNetworkReachabilityStatusUnknown = -1, 未知
AFNetworkReachabilityStatusNotReachable = 0, 不可用
AFNetworkReachabilityStatusReachableViaWWAN = 1, 手机自带网络
AFNetworkReachabilityStatusReachableViaWiFi = 2, wifi
*/
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"不可用");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"手机自带网络");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"Wifi");
break;
default:
break;
}
}];
// 开始监听
[manager startMonitoring];
网友评论