1、网络监测
/** * 网络检测 */ - (void)networkingMonitoring { //打开网络监测 [[AFNetworkReachabilityManager sharedManager] startMonitoring]; [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { if (status == AFNetworkReachabilityStatusUnknown) { NSLog(@"网络未知"); } if (status == AFNetworkReachabilityStatusNotReachable) { NSLog(@"无网络访问"); } if (status == AFNetworkReachabilityStatusReachableViaWWAN) { NSLog(@"手机网络"); } if (status == AFNetworkReachabilityStatusReachableViaWiFi) { NSLog(@"WiFi网络"); } }]; }
2、网络请求
get请求
- (void)requestTool:(NSString *)url { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { NSLog(@"下载的速度"); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"请求成功"); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"错误信息:%@",error); }]; }
post请求
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
3、下载
/** * AFN3.0 下载 */ - (void)downLoad{ //1.创建管理者对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //2.确定请求的URL地址 NSURL *url = [NSURL URLWithString:@""]; //3.创建请求对象 NSURLRequest *request = [NSURLRequest requestWithURL:url]; //下载任务 NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { //打印下下载进度 WKNSLog(@"%lf",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount); } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { //下载地址 WKNSLog(@"默认下载地址:%@",targetPath); //设置下载路径,通过沙盒获取缓存地址,最后返回NSURL对象 NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]; return [NSURL URLWithString:filePath]; } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { //下载完成调用的方法 WKNSLog(@"下载完成:"); WKNSLog(@"%@--%@",response,filePath); }]; //开始启动任务 [task resume]; }
4、上传
/** * AFN 3.0 上传 * 有两种方式 * upLoad1 和 upLoad2 */ //第一种方法是通过工程中的文件进行上传 - (void)upLoad1{ //1。创建管理者对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //2.上传文件 NSDictionary *dict = @{@"username":@"1234"}; NSString *urlString = @"22222"; [manager POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { //上传文件参数 UIImage *iamge = [UIImage imageNamed:@"123.png"]; NSData *data = UIImagePNGRepresentation(iamge); //这个就是参数 [formData appendPartWithFileData:data name:@"file" fileName:@"123.png" mimeType:@"image/png"]; } progress:^(NSProgress * _Nonnull uploadProgress) { //打印下上传进度 WKNSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { //请求成功 WKNSLog(@"请求成功:%@",responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { //请求失败 WKNSLog(@"请求失败:%@",error); }]; } //第二种是通过URL来获取路径,进入沙盒或者系统相册等等 - (void)upLoda2{ //1.创建管理者对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //2.上传文件 NSDictionary *dict = @{@"username":@"1234"}; NSString *urlString = @"22222"; [manager POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"文件地址"] name:@"file" fileName:@"1234.png" mimeType:@"application/octet-stream" error:nil]; } progress:^(NSProgress * _Nonnull uploadProgress) { //打印下上传进度 WKNSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { //请求成功 WKNSLog(@"请求成功:%@",responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { //请求失败 WKNSLog(@"请求失败:%@",error); }]; }
上传下载感谢简书WK_IOSDevelpoer
时间: 2024-10-21 23:01:53