文丨清楓
AFNetworking
開源庫封裝了原生的方法齐鲤,由于NSURLConnection
的棄用彼念,AFNetworking
的使用也有一些改變纽谒。
Get請求
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"這里打印請求成功要做的事");
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error); //這里打印錯(cuò)誤信息
}];
Post請求
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSMutableDictionary *parameters = @{@"":@"",@"":@""};
[manager POST:URL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
Download請求
//創(chuàng)建NSURLSessionConfiguration
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//創(chuàng)建管理者對象
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//設(shè)置url
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
//創(chuàng)建請求對象
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//下載任務(wù)
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//打印下下載進(jìn)度
NSLog(@"%lf",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
//設(shè)置下載路徑
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
//下載完成
NSLog(@"File downloaded to: %@", filePath);
}];
//啟動任務(wù)
[downloadTask resume];
Upload請求
//創(chuàng)建NSURLSessionConfiguration
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//創(chuàng)建管理者對象
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//設(shè)置url
NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
//創(chuàng)建請求對象
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//文件路徑
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
//上傳任務(wù)
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
//錯(cuò)誤
NSLog(@"Error: %@", error);
} else {
//成功
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
//啟動任務(wù)
[uploadTask resume];
網(wǎng)絡(luò)狀態(tài)
//1.創(chuàng)建網(wǎng)絡(luò)監(jiān)測者
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//監(jiān)測網(wǎng)絡(luò)改變
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知網(wǎng)絡(luò)狀態(tài)");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"無網(wǎng)絡(luò)");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"蜂窩數(shù)據(jù)網(wǎng)");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi網(wǎng)絡(luò)");
break;
default:
break;
}
}] ;
}