AFN官网翻译

  AFNetworking.xcworkspace 重命名AFNetworking Mac的例子来AFNetworking OS X示例 a month ago
  AFNetworking 使用来自AssertMacros.h内置__Require宏 13 days ago
  轻微的重构 16 days ago
  测试 增加了单元测试来测试的bug。 a month ago
  的UIKit + AFNetworking 更新的UIButton + AFNetworking.m 4 days ago
  .cocoadocs.yml 更新.cocoadocs.yml 2 months ago
  的.gitignore 添加测试/ Podfile.lock到的.gitignore a year ago
  .travis.yml 取出冲泡更新,S /测试:IOS /测试 10 months ago
  AFNetworking.podspec 碰撞版本2.5.0 29 days ago
  变化 碰撞版本2.5.0 29 days ago
  CONTRIBUTING.md 更新CONTRIBUTING.md 7 months ago
  许可 更新执照一年 9 months ago
  README.md 更新README.md a month ago
  Rake文件 构建之前干净 10 months ago

 README.md

AFNetworking

构建状态

AFNetworking是为iOS和Mac OS X.它是建立在之上一个愉快的网络图书馆基金会网址加载系统,扩展内置可可强大的高层次的网络抽象。它有一个模块化的架构,精心设计,功能丰富的API,是一个欢乐的使用。

也许所有最重要的功能,但是,是谁使用,并有助于AFNetworking每天开发了惊人的社区。AFNetworking权力一些iPhone,iPad和Mac上最流行和广受好评的应用程序。

选择AFNetworking你的下一个项目,或迁移了现有的项目 - 你很高兴你没有!

如何开始

沟通

  • 如果你需要帮助,使用堆栈溢出(标签'afnetworking“)
  • 如果你想提出一个一般性的问题,使用堆栈溢出
  • 如果你发现了一个bug并能提供的步骤可靠地重现它,打开一个问题。
  • 如果你有一个功能请求,打开一个问题。
  • 如果你想参与,提交pull请求。

安装与CocoaPods

CocoaPods是一个依赖经理的Objective-C,其自动化并简化在项目中使用第三方库,比如AFNetworking的过程。请参阅“入门”指南以获取更多信息

Podfile
平台:IOS' 7.0 ' AFNetworking 〜> 2.0 

要求

AFNetworking版本最小的iOS目标最低OS X目标笔记
2.X的iOS 6OS X 10.8Xcode中5是必需的。AFHTTPSessionManager需要iOS 7或OS X 10.9。
1.x中的iOS 5的Mac OS X 10.7 
0.10.xiOS 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
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];
创建上传任务的多部分请求,与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]];
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
URL形式参数编码
[[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
JSON参数编码
[[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));
}];
HTTP经理可达
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连接钉扎配置的所有沟通和启用。

让无效的SSL证书
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安装,你可以通过执行套房耙测试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值