README.md
AFNetworking是为iOS和Mac OS X.它是建立在之上一个愉快的网络图书馆基金会网址加载系统,扩展内置可可强大的高层次的网络抽象。它有一个模块化的架构,精心设计,功能丰富的API,是一个欢乐的使用。
也许所有最重要的功能,但是,是谁使用,并有助于AFNetworking每天开发了惊人的社区。AFNetworking权力一些iPhone,iPad和Mac上最流行和广受好评的应用程序。
选择AFNetworking你的下一个项目,或迁移了现有的项目 - 你很高兴你没有!
如何开始
- 下载AFNetworking并尝试了包括Mac和iPhone应用程序的例子
- 阅读“入门”指南,常见问题解答,或其他物品上的Wiki
- 检查出的文档进行全面审视所有的API提供AFNetworking
- 阅读AFNetworking 2.0迁移指南为1.0的体系结构更改的概述。
沟通
- 如果你需要帮助,使用堆栈溢出。(标签'afnetworking“)
- 如果你想提出一个一般性的问题,使用堆栈溢出。
- 如果你发现了一个bug,并能提供的步骤可靠地重现它,打开一个问题。
- 如果你有一个功能请求,打开一个问题。
- 如果你想参与,提交pull请求。
安装与CocoaPods
CocoaPods是一个依赖经理的Objective-C,其自动化并简化在项目中使用第三方库,比如AFNetworking的过程。请参阅“入门”指南以获取更多信息。
Podfile
平台:IOS,' 7.0 ' 荚“ AFNetworking “,“〜> 2.0 “
要求
AFNetworking版本 | 最小的iOS目标 | 最低OS X目标 | 笔记 |
---|---|---|---|
2.X | 的iOS 6 | OS X 10.8 | Xcode中5是必需的。AFHTTPSessionManager 需要iOS 7或OS X 10.9。 |
1.x中 | 的iOS 5 | 的Mac OS X 10.7 | |
0.10.x | iOS 4的 | 的Mac OS X 10.6 |
(OS X项目必须支持64位与现代可可运行)。
架构
NSURLConnection的
AFURLConnectionOperation
AFHTTPRequestOperation
AFHTTPRequestOperationManager
NSURLSession (的iOS 7 / Mac的OS X 10.9)
AFURLSessionManager
AFHTTPSessionManager
序列化
<AFURLRequestSerialization>
AFHTTPRequestSerializer
AFJSONRequestSerializer
AFPropertyListRequestSerializer
<AFURLResponseSerialization>
AFHTTPResponseSerializer
AFJSONResponseSerializer
AFXMLParserResponseSerializer
AFXMLDocumentResponseSerializer
键(Mac OS X)AFPropertyListResponseSerializer
AFImageResponseSerializer
AFCompoundResponseSerializer
附加功能
AFSecurityPolicy
AFNetworkReachabilityManager
用法
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
URL,表单编码的请求
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); }];
AFURLSessionManager
creates and manages an
NSURLSession
object based on a specified
NSURLSessionConfiguration
object, which conforms to
<NSURLSessionTaskDelegate>
,
<NSURLSessionDataDelegate>
,
<NSURLSessionDownloadDelegate>
, and
<NSURLSessionDelegate>
.
翻译:AFURLSessionManager
创建和管理一个NSURLSession
根据指定对象NSURLSessionConfiguration
对象,它符合<NSURLSessionTaskDelegate>
,<NSURLSessionDataDelegate>
,<NSURLSessionDownloadDelegate>
,和<NSURLSessionDelegate>
。
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];
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];
NSString *URLString = @"http://example.com"; NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};
[[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:URLString parameters:parameters error:nil];
GET http://example.com?foo=bar&baz[]=1&baz[]=2&baz[]=3
[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];
POST http://example.com/
Content-Type: application/x-www-form-urlencoded
foo=bar&baz[]=1&baz[]=2&baz[]=3
[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];
POST http://example.com/
Content-Type: application/json
{"foo": "bar", "baz": [1,2,3]}
AFNetworkReachabilityManager
监控领域的可达性,并为WWAN和WiFi网络接口地址。
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status)); }];
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; } }]; [manager.reachabilityManager startMonitoring];
AFSecurityPolicy
评估服务器信任寄托对X.509证书和通过安全连接的公共密钥。
增加固定SSL证书到您的应用程序有助于防止人在这方面的中间人攻击和其他安全漏洞。应用程序处理敏感的客户数据或财务信息我们强烈建议路线在使用SSL的HTTPS连接钉扎配置的所有沟通和启用。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production不推荐用于生产
AFHTTPRequestOperation
AFHTTPRequestOperation
是的一个子类AFURLConnectionOperation
使用HTTP或HTTPS协议请求。它封装的可接受状态码和内容的类型,确定一个请求的成功或失败的概念。
虽然AFHTTPRequestOperationManager
通常是去约发出请求的最佳方式,AFHTTPRequestOperation
可以单独使用。
GET
与AFHTTPRequestOperation
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];
AFNetworking包括一套内部测试子目录单元测试。为了运行单元测试,您必须通过安装测试依赖CocoaPods:
$ CD测试
$pod install //吊舱安装
一旦测试安装的依赖,你可以通过'iOS的测试“,并在Xcode'OS X测试”计划,执行测试套件。
从命令行运行测试
测试也可以在命令行或在持续集成环境中运行。该xcpretty
实用程序需要运行在命令行测试之前安装:
$宝石安装xcpretty
一旦xcpretty
安装,你可以通过执行套房耙测试
。