AFNetworking是一個(gè)基于iOS和Mac OS X的備受人們喜愛的網(wǎng)絡(luò)類庫馍刮。它是建立在 Foundation URL Loading System之上,將高層網(wǎng)絡(luò)進(jìn)行強(qiáng)有力的擴(kuò)展抽象成了Cocoa因苹。它有一個(gè)精心設(shè)計(jì)的模塊結(jié)構(gòu)、各種易用且功能豐富的API。
然而最令人吃驚的不是AFNetworking的這些特點(diǎn)驱闷,而是每天都有開發(fā)者使用并維護(hù)著。AFNetworking應(yīng)用在iPhone空免、iPad空另、mac的App上。
為你下一個(gè)項(xiàng)目選擇AFNetworking或者將他移植到現(xiàn)有的項(xiàng)目中都會是你受益匪淺蹋砚。
怎么開始
- 下載AFNetworking并且在mac和iPhone上嘗試一些示例App
- 讀入門指南扼菠、常見問題的解答、或其wiki上的相關(guān)文章
- 查看AFNetworking文檔中所有可用的API
- 閱讀概述AFNetworking從2.0到3.0結(jié)構(gòu)變化的遷移指南
交流
- 如果你需要幫助坝咐,用Stack Overflow循榆。
- 如果你想要問問題,用Stack Overflow墨坚。
- 如果你發(fā)現(xiàn)了一個(gè)bug并且可以提供操作步驟復(fù)現(xiàn)它秧饮,公布這個(gè)問題
- 如果你有一個(gè)指定的需求,公布它
- 如果你想要投稿泽篮,提交一個(gè)pull請求
安裝
AFNetworking支持在一個(gè)項(xiàng)目中安裝的多種方法
用Cocoapods安裝
CocoaPods依賴objective - c管理,自動化和簡化的過程像AFNetworking在你的項(xiàng)目使用第三方庫盗尸。有關(guān)更多信息,請參見“入門”指南。你可以用下面的命令安裝:
$ gem install cocoapods
Podfile
使用CocoaPods 將AFNetworking導(dǎo)入到你的Xcode項(xiàng)目,在Podfile輸入:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target 'TargetName' do
pod 'AFNetworking', '~> 3.0'
end
然后執(zhí)行命令
$ pod install
用Carthage安裝
Carthage是一個(gè)分散的依賴管理器,構(gòu)建你的依賴關(guān)系,為您提供二進(jìn)制框架帽撑。
你可以用 Homebrew安裝Carthage,然后運(yùn)行一下代碼
$ brew update
$ brew install carthage
用Carthage導(dǎo)入AFNetworking到你的工程中泼各,指定你的Cartfile文件
github "AFNetworking/AFNetworking" ~> 3.0
運(yùn)行Carthage 構(gòu)建framework并且拖動AFNetworking的framework到你的工程中
要求
AFNetworking 版本 | iOS最低系統(tǒng) | OS X 最低系統(tǒng) | watchOS 最低系統(tǒng) | tvOS 最低系統(tǒng) | 注釋 |
---|---|---|---|---|---|
3.x | iOS 7 | OS X 10.9 | watchOS 2.0 | tvOS 9.0 | 必需xcode7以上 ;NSURLConnectionOperation已經(jīng)被刪除了 |
2.6 -> 2.6.3 | iOS 7 | OS X 10.9 | watchOS 2.0 | n/a | 必需xcode7以上 |
2.0 -> 2.5.4 | iOS 6 | OS X 10.8 | n/a | n/a | 必需xcode5 以上 |
1.x | iOS 5 | Mac OS X 10.7 | n/a | n/a | n/a |
0.10.x | iOS 4 | Mac OS X 10.6 | n/a | n/a | n/a |
架構(gòu)
NSURLSession
- AFURLSessionManager
- AFHTTPSessionManager
Serialization
- <AFURLRequestSerialization>
- AFHTTPRequestSerializer
- AFJSONRequestSerializer
- AFPropertyListRequestSerializer
- <AFURLResponseSerialization>
- AFHTTPResponseSerializer
- AFJSONResponseSerializer
- AFXMLParserResponseSerializer
- AFXMLDocumentResponseSerializer (Mac OS X)
- AFPropertyListResponseSerializer
- AFImageResponseSerializer
- AFCompoundResponseSerializer
Additional Functionality
- AFSecurityPolicy
- AFNetworkReachabilityManager
用法
AFURLSessionManager
AFURLSessionManager創(chuàng)建并管理一個(gè)NSURLSession對象亏拉,基于一個(gè)指定的NSURLSessionConfiguration對象扣蜻,這個(gè)對象要遵循以下協(xié)議<NSURLSessionTaskDelegate>, <NSURLSessionDataDelegate>, <NSURLSessionDownloadDelegate>, and <NSURLSessionDelegate>.
創(chuàng)建下載任務(wù)
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];
創(chuàng)建上傳任務(wù)
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];
創(chuàng)建一個(gè)復(fù)雜請求的上傳任務(wù)逆巍,附帶進(jìn)度條
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];
創(chuàng)建一個(gè)數(shù)據(jù)任務(wù)
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];
請求序列化
請求序列化從請求一個(gè)URL字符串開始創(chuàng)建,編碼參數(shù)弱贼,查找字符串或者HTTP的body
NSString *URLString = @"http://example.com";
NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};
查找字符參數(shù)編碼
[[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:URLString parameters:parameters error:nil];
URL表單參數(shù)編碼
[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];
POST http://example.com/
Content-Type: application/x-www-form-urlencoded
foo=bar&baz[]=1&baz[]=2&baz[]=3
JSON參數(shù)編碼
[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];
POST http://example.com/
Content-Type: application/json
{"foo": "bar", "baz": [1,2,3]}
監(jiān)測網(wǎng)絡(luò)管理器
AFNetworkReachabilityManager 監(jiān)測域名以及IP地址的暢通性蒸苇,對于WWAN以及WiFi的網(wǎng)絡(luò)接口都管用。
- 不要用網(wǎng)絡(luò)可用來確定原始請求是否應(yīng)該被發(fā)送吮旅。
- 你可以用網(wǎng)絡(luò)可用決定當(dāng)一個(gè)請求是否應(yīng)該自動重試溪烤。
- 網(wǎng)絡(luò)可用性是一個(gè)有用的工具來決定一個(gè)請求可能會失敗的原因。
分享網(wǎng)絡(luò)可用性
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
}];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
安全策略
AFSecurityPolicy評估對固定X服務(wù)器信任庇勃。509證書和公鑰安全連接檬嘀。將固定的SSL證書添加到您的應(yīng)用程序可以幫助防止中間人攻擊和其他漏洞≡鹑拢或財(cái)務(wù)信息處理敏感的客戶數(shù)據(jù)的應(yīng)用程序被強(qiáng)烈鼓勵所有通信路由在一個(gè)HTTPS和SSL連接固定配置和啟用鸳兽。
允許不合法的SSL證書
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production
單元測試
AFNetworking包括一套單元測試中的測試子目錄。這些測試可以在平臺上運(yùn)行簡單地執(zhí)行測試活動你想測試框架罕拂。
工作人員
AFNetworking 是由Alamofire Software Foundation 所有并維護(hù)
AFNetworking 最初是由Scott Raymond和 Mattt Thompson在Gowalla for iPhone 的開發(fā)中創(chuàng)建的
AFNetworking的logo是由Alan Defibaugh設(shè)計(jì)的
最后 謝謝所有為AFNetworking做出貢獻(xiàn)的人
安全信息披露
如果你相信你已經(jīng)確定了一個(gè)與AFNetworking安全漏洞,你應(yīng)該報(bào)告盡快通過電子郵件至security@alamofire.org揍异。請不要發(fā)布到一個(gè)公共問題跟蹤器。
證書
AFNetworking MIT許可下發(fā)布爆班。有關(guān)詳細(xì)信息,請參閱許可證衷掷。
PS:翻譯完感覺更不像人話了 哈哈
GitHub原文網(wǎng)址:https://github.com/AFNetworking/AFNetworking