AFURLSessionManager 上傳下載使用代碼說(shuō)明
1、下載 Creating a Download Task
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
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);
}];
[downloadTask resume];
2、上傳 Creating an Upload Task
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];
3、批量上傳 Creating an Upload Task for a Multi-Part Request, with Progress
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[progressView setProgress:uploadProgress.fractionCompleted];
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
4、數(shù)據(jù)任務(wù) Creating a Data Task
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[dataTask resume];
5、請(qǐng)求參數(shù)設(shè)置 Request Serialization
Request serializers create requests from URL strings, encoding parameters as either a query string or HTTP body.
NSString *URLString = @"http://example.com";
NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};
總結(jié)
以上所述是小編給大家介紹的AFURLSessionManager 上傳下載使用代碼說(shuō)明,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Android自定義View繪制隨機(jī)生成圖片驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android自定義View繪制隨機(jī)生成圖片驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android實(shí)現(xiàn)捕獲未知異常并提交給服務(wù)器的方法
這篇文章主要介紹了Android實(shí)現(xiàn)捕獲未知異常并提交給服務(wù)器的方法,涉及Android的異常與錯(cuò)誤處理機(jī)制相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android百度地圖自定義公交路線(xiàn)導(dǎo)航
這篇文章主要介紹了Android百度地圖自定義公交路線(xiàn)導(dǎo)航的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android實(shí)現(xiàn)動(dòng)態(tài)改變app圖標(biāo)的示例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)改變app圖標(biāo)的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
Android實(shí)現(xiàn)MVVM架構(gòu)數(shù)據(jù)刷新詳解流程
MVVM架構(gòu)模式,即Model-View-ViewModel三個(gè)層級(jí),MVVM模式出來(lái)的時(shí)間已經(jīng)很長(zhǎng)了,網(wǎng)上關(guān)于MVVM模式的解析也有很多,我這里只說(shuō)一下我自己的理解,基本上是和MVP模式相比較的一個(gè)差異2021-10-10
RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局
這篇文章主要為大家詳細(xì)介紹了RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Android 中ListView和GridView賦值錯(cuò)位
這篇文章主要介紹了Android 中ListView和GridView賦值錯(cuò)位的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10

