测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2016年8月 |
由 gaoxiao228 维护。
依赖项 | |
EachbabyGetAdList | >= 0 |
TOWebViewController | ~> 2.2.3 |
Masonry | ~> 0.6.4 |
AFNetworking 是一个令人愉悦的 iOS 和 Mac OS X 网络库。它建立在 Foundation URL 加载系统之上,扩展了 Cocoa 中内置的强大高级网络抽象。它具有模块化架构,具备精心设计、特点丰富的 API,使用起来非常愉快。
然而,最重要的可能还是每天使用并贡献 AFNetworking 的开发者社区。AFNetworking 为 iPhone、iPad 和 Mac 上一些最流行和广受好评的应用程序提供了动力。
选择 AFNetworking 作为您下一个项目的库,或者将现有项目迁移过来——您会为此感到高兴的!
CocoaPods 是一个 Objective-C 的依赖关系管理器,它自动化并简化了在项目中使用像 AFNetworking 这样的第三方库的过程。有关更多信息,请参阅"如何开始"指南。
platform :ios, '7.0'
pod "AFNetworking", "~> 2.0"
AFNetworking 版本 | 最小 iOS 目标 | 最小 OS X 目标 | 注意 |
---|---|---|---|
2.x | iOS 6 | OS X 10.8 | 需要 Xcode 5。`NSURLSession` 子规范需要 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位带有现代 Cocoa 运行时)。
使用 Swift 进行编程?试试 Alamofire 以获得更传统的一组 API。
AFURLConnectionOperation
AFHTTPRequestOperation
AFHTTPRequestOperationManager
AFURLSessionManager
AFHTTPSessionManager
<AFURLRequestSerialization>
AFHTTPRequestSerializer
AFJSONRequestSerializer
AFPropertyListRequestSerializer
<AFURLResponseSerialization>
AFHTTPResponseSerializer
AFJSONResponseSerializer
AFXMLParserResponseSerializer
AFXMLDocumentResponseSerializer
(Mac OS X)AFPropertyListResponseSerializer
AFImageResponseSerializer
AFCompoundResponseSerializer
AFSecurityPolicy
AFNetworkReachabilityManager
AFHTTPRequestOperationManager
封装了通过 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-Form-Encoded 请求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
根据指定的 NSURLSessionConfiguration
对象创建并管理一个 NSURLSession
对象,该对象遵守 <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];
请求序列化器从 URL 字符串创建请求,将参数编码为查询字符串或 HTTP 请求正文。
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
是AFURLConnectionOperation
的子类,用于使用HTTP或HTTPS协议进行请求。它封装了可接受的状态码和内容类型的概念,这些可决定请求的成功或失败。
尽管通常最好使用AFHTTPRequestOperationManager
来发出请求,但AFHTTPRequestOperation
也可独立使用。
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在Tests子目录中包含了一系列单元测试。为了运行单元测试,您必须通过CocoaPods安装测试依赖项。
$ cd Tests
$ pod install
测试依赖项安装完成后,您可以通过Xcode中的"iOS Tests"和"OS X Tests"方案执行测试套件。
测试还可以从命令行或持续集成环境中运行。在从命令行运行测试之前,需要安装xcpretty
实用程序。
$ gem install xcpretty
安装xcpretty
后,您可以通过rake test
命令执行套件。
AFNetworking由Alamofire软件基金会拥有和维护。
AFNetworking最初由Scott Raymond和Mattt Thompson在iPhone应用Gowalla开发中创建。
AFNetworking的标志由Alan Defibaugh设计。
最重要的是,感谢AFNetworking不断增加的贡献者列表。
如果您认为发现了AFNetworking的安全漏洞,请尽快通过电子邮件报告给[email protected]。请不要在公共问题跟踪器中发布。
AFNetworking在MIT许可下发布。有关详细信息,请参阅LICENSE。