AFNetworking是一個為?iOS?和?Mac OSX?制作的令人愉快的網絡庫,它建立在URL?裝載系統框架的頂層,內置在Cocoa里,擴展了強有力的高級網絡抽象。它的模塊架構被良好的設計,擁有豐富的功能,因此,使用起來,必定賞心悅目。
? ? ? ?@原文鏈接https://github.com/AFNetworking/AFNetworking,我在此基礎上了點配置修改
? ? ? ?@介紹
1.支持HTTP請求和基于REST的網絡服務(包括GET、POST、 PUT、DELETE等)
2.支持ARC
3.要求iOS 5.0及以上版本
4.UIKit擴展
? ? ? ?@配置
? ? ? ?1.下載[AFNetworking](http://afnetworking.com/),將2個文件夾:AFNetworking和UIKit+AFNetworking拖入工程
? ? ? ?2.導入以下庫文件:CFNetwork、Security、SystemConfiguration、MobileCoreServices
? ? ? ?3.如果你以前用的是1.0版本,那么[AFNetworking 2.0 Migration Guide](https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-2.0-Migration-Guide)能幫助你
? ? ? ?4.如果你是用CocoaPods配置的,那么
? ? ? ? ? ?platform:ios,'7.0'
? ? ? ? ? ?pod"AFNetworking","~>2.0"
? ? ? ? ? ?@使用
? ? ? ? ? ?1.HTTP請求操作
? ? ? ? ? ?AFHTTPRequestOperationManager封裝的共同模式與web應用程序通過HTTP通信,包括創建請求,響應序列化,網絡可達性監控、運營管理和安全,以及請求。
? ? ? ? ? ?GET請求
~~~
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
~~~
? ? ? ? ? POST請求
~~~
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
~~~
? ? ? ? ? ?POST請求(多表)
~~~
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
~~~
? ? ? ? ? ? ?2.AFURLSessionManager(NSURLSession詳細見[網絡編程(6)](http://blog.csdn.net/hmt20130412/article/details/29412349))
? ? ? ? ? ? ?創建和管理制定的NSURLSession對象NSURLSessionConfiguration對象必須實現, , , 協議
? ? ? ? ? ? ? 創建一個下載任務
~~~
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];
~~~
? ? ? ??? ? ? ?創建一個上傳任務
~~~
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];
~~~
? ? ? ? ? ? ?創建一個帶多表,進度的上傳任務
~~~
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]];
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
~~~
? ? ? ? ? ? ? 創建一個數據流Data任務
~~~
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
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];
~~~
? ? ? ? ? ? ? ? 3.網絡監測(一般會用另一個網絡監測類,Reachability,還有JSON解析方法,反正我也一般不用,自行腦補)
? ? ? ? ? ? ? ? AFNetworkReachabilityManager監控網絡領域的可達性,WWAN地址和WiFi接口.
? ? ? ? ? ? ? ?當前網絡狀態
~~~
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
}];
~~~
? ? ? ? ? ? ? HTTP Manager 可達性
~~~
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
[operationQueue setSuspended:NO];
break;
case AFNetworkReachabilityStatusNotReachable:
default:
[operationQueue setSuspended:YES];
break;
}
}];
~~~
? ? ? ? ? ? ? ? ? 4.AFHTTPRequestOperation
? ? ? ? ? ? ? ? ? AFHTTPRequestOperation是使用HTTP或HTTPS協議的AFURLConnectionOperation的子類。
它封裝的獲取后的HTTP狀態和類型將決定請求的成功與否。雖然AFHTTPRequestOperationManager通常是最好的去請求的方式,但是AFHTTPRequestOpersion也能夠單獨使用。
? ? ? ? ? ? ? ? ?GET請求
~~~
NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:op];
~~~
? ? ? ? ? ? ? ?批量多請求
~~~
NSMutableArray *mutableOperations = [NSMutableArray array];
for (NSURL *fileURL in filesToUpload) {
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[mutableOperations addObject:operation];
}
NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
NSLog(@"All operations in batch complete");
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
~~~
? ? ? ? ? ? ? ? ? @其他資料
? ? ? ? ? ? ? ? ?1.[官網](http://afnetworking.com/)
? ? ? ? ? ? ? ? ?2.[英文文檔](http://cocoadocs.org/docsets/AFNetworking/2.0.3/)
- 前言
- 沙盒機制與文件(一)
- 沙盒機制和文件(二)
- 沙盒機制和文件(三)
- NSBundle介紹以及讀取沙盒文件路徑問題
- 數據持久化(一)-----歸檔 讀寫 文件路徑
- 數據持久化(二)-----Sqlite
- 數據持久化(三)使用第三方類庫FMDB
- 數據持久化(四)之NSUserDefaults
- 數據持久化(五)之CoreData
- 數據持久化(六)之Using CoreData with MagicalRecord
- 數據解析(一)解析XML之系統自帶NSXMLParse類
- 數據解析(二)解析XML之GDataXMLNode
- 數據解析(三)解析JSON-----系統自帶NSJSONSerialization 與 第三方JSONKit
- iOS多線程編程(一)NSThread
- iOS多線程編程(二)NSOperationQueue
- iOS多線程編程(三)Grand Central Dispatch(GCD)詳解
- iOS網絡編程(一)NSURLConnection
- iOS網絡編程(二) 自定義請求網絡類----推薦用于需要請求過程片段數據
- iOS網絡編程(三) 異步加載及緩存圖片---->SDWebImage
- iOS網絡編程(四) 異步加載及緩存圖片-----自定義類
- iOS網絡編程(五) 異步加載及緩存圖片-----EGO
- iOS網絡編程(六) NSURLSession詳解
- iOS網絡編程(7) 第三方開源庫----->AFNetworking