資料來源 : http://github.ibireme.com/github/list/ioshtml
GitHub : 連接地址ios
簡介 :git
A delightful iOS and OS X networking framework.github
推薦參考 : web
http://afnetworking.comjson
http://www.aiuxian.com/article/p-1537192.htmlxcode
連接地址1、 文件目錄
連接地址1. AFNetworking 目錄內容

連接地址2. UIKit+AFNetworking 目錄內容

連接地址3. 關聯關係(AFNetworking)

連接地址2、 詳細介紹
連接地址1. AFNetworking
這是 AFNetworking 的主要部分,包括 6 個功能部分共 9 個類。安全
連接地址1)AFNetworking.h
- #import <Foundation/Foundation.h>
- #import <Availability.h>
-
- #ifndef _AFNETWORKING_
- #define _AFNETWORKING_
-
- #import "AFURLRequestSerialization.h"
- #import "AFURLResponseSerialization.h"
- #import "AFSecurityPolicy.h"
- #import "AFNetworkReachabilityManager.h"
-
- #import "AFURLConnectionOperation.h"
- #import "AFHTTPRequestOperation.h"
- #import "AFHTTPRequestOperationManager.h"
-
- #if ( ( defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090) || \
- ( defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 ) )
- #import "AFURLSessionManager.h"
- #import "AFHTTPSessionManager.h"
- #endif
-
- #endif /* _AFNETWORKING_ */
這是 AFNetworking 的公共頭文件,在使用 AFNetworking 庫時可直接在 Prefix.pch 文件中引入,或者在工程的網絡管理模塊相關文件中引入。 網絡
連接地址2)AFSecurityPolicy.h
- @interface AFSecurityPolicy : NSObject
-
- @property (nonatomic, assign) AFSSLPinningMode SSLPinningMode;
-
- @property (nonatomic, assign) BOOL validatesCertificateChain;
-
- @property (nonatomic, strong) NSArray *pinnedCertificates;
-
- @property (nonatomic, assign) BOOL allowInvalidCertificates;
-
- @property (nonatomic, assign) BOOL validatesDomainName;
這個類主要是爲網絡請求添加 SSL 安全驗證, SSL 安全驗證類型有以下三種,默認是 AFSSLPinningModeNone 類型,另外經過 SSL 證書和密鑰能夠增長請求的安全性,避免請求被劫持和攻擊。session
- typedef NS_ENUM(NSUInteger, AFSSLPinningMode) {
- AFSSLPinningModeNone,
- AFSSLPinningModePublicKey,
- AFSSLPinningModeCertificate,
- };
關於 SSL 和數字證書相關可參考這裏(SSL)和這裏(數字證書)。
連接地址3)AFNetworkReachabilityManager.h
- @interface AFNetworkReachabilityManager : NSObject
-
- @property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
-
- @property (readonly, nonatomic, assign, getter = isReachable) BOOL reachable;
-
- @property (readonly, nonatomic, assign, getter = isReachableViaWWAN) BOOL reachableViaWWAN;
-
- @property (readonly, nonatomic, assign, getter = isReachableViaWiFi) BOOL reachableViaWiFi;
這個類和蘋果官方提供的 Reachability 類功能相似,可是功能更增強大,不只增長了更多的公共屬性,也增長了狀態變動閉包(block)操做,還增長了通知標誌串,用過 Reachability 應該可以很快理解並愛上這個類。
連接地址4)AFURLConnectionOperation.h
- @interface AFURLConnectionOperation : NSOperation <NSURLConnectionDelegate, NSURLConnectionDataDelegate, NSCoding, NSCopying>
-
-
- @property (nonatomic, strong) NSSet *runLoopModes;
-
-
- @property (readonly, nonatomic, strong) NSURLRequest *request;
-
- @property (readonly, nonatomic, strong) NSURLResponse *response;
-
- @property (readonly, nonatomic, strong) NSError *error;
-
-
- @property (readonly, nonatomic, strong) NSData *responseData;
-
- @property (readonly, nonatomic, copy) NSString *responseString;
-
- @property (readonly, nonatomic, assign) NSStringEncoding responseStringEncoding;
-
-
- @property (nonatomic, assign) BOOL shouldUseCredentialStorage;
-
- @property (nonatomic, strong) NSURLCredential *credential;
-
-
- @property (nonatomic, strong) AFSecurityPolicy *securityPolicy;
-
-
- @property (nonatomic, strong) NSInputStream *inputStream;
-
- @property (nonatomic, strong) NSOutputStream *outputStream;
-
-
- @property (nonatomic, strong) dispatch_queue_t completionQueue;
-
- @property (nonatomic, strong) dispatch_group_t completionGroup;
-
-
- @property (nonatomic, strong) NSDictionary *userInfo;
這是一個 NSOperation 子類,它實現了 NSURLConnection 的所有代理方法,所執行的是單個網絡請求的操做。
連接地址5)AFHTTPRequestOperation.h
- @interface AFHTTPRequestOperation : AFURLConnectionOperation
-
-
- @property (readonly, nonatomic, strong) NSHTTPURLResponse *response;
-
- @property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;
-
- @property (readonly, nonatomic, strong) id responseObject;
這是 AFURLConnectionOperation 的子類,主要針對 HTTP 和 HTTPS 類型的請求,這也是最經常使用的請求操做。
連接地址6)AFHTTPRequestOperationManager.h
- @interface AFHTTPRequestOperationManager : NSObject <NSCoding, NSCopying>
-
- @property (readonly, nonatomic, strong) NSURL *baseURL;
-
- @property (nonatomic, strong) AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer;
-
- @property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;
-
- @property (nonatomic, strong) NSOperationQueue *operationQueue;
-
-
- @property (nonatomic, assign) BOOL shouldUseCredentialStorage;
-
- @property (nonatomic, strong) NSURLCredential *credential;
-
-
- @property (nonatomic, strong) AFSecurityPolicy *securityPolicy;
-
-
- @property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager;
這是 AFHTTPRequestOperation 的一個管理類,細化了不一樣類型的請求操做( GET、HEAD、POST、PUT、PATCH、DELETE ),經過這個管理類建立的網絡請求操做都會被加入到 operationQueue 中執行。
連接地址7)AFURLSessionManager.h
- @interface AFURLSessionManager : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSCoding, NSCopying>
-
- @property (readonly, nonatomic, strong) NSURLSession *session;
-
- @property (readonly, nonatomic, strong) NSOperationQueue *operationQueue;
-
- @property (nonatomic, strong) id <AFURLResponseSerialization> responseSerializer;
-
-
- @property (nonatomic, strong) AFSecurityPolicy *securityPolicy;
-
-
- @property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager;
-
-
- @property (readonly, nonatomic, strong) NSArray *tasks;
-
- @property (readonly, nonatomic, strong) NSArray *dataTasks;
-
- @property (readonly, nonatomic, strong) NSArray *uploadTasks;
-
- @property (readonly, nonatomic, strong) NSArray *downloadTasks;
-
-
- @property (nonatomic, strong) dispatch_queue_t completionQueue;
-
- @property (nonatomic, strong) dispatch_group_t completionGroup;
這是 AFNetworking 實現的 NSURLSession 的一個管理類,在這個類裏面已經實現了所有相關的 NSURLSession 代理方法,NSURLSession 是 iOS7 新增長的用於網絡請求相關的任務類,具體可參考 這裏(蘋果官方文檔) 、 這裏(相關博客一) 和 這裏(相關博客二) 。
連接地址8)AFHTTPSessionManager.h
- @interface AFHTTPSessionManager : AFURLSessionManager <NSCoding, NSCopying>
-
- @property (readonly, nonatomic, strong) NSURL *baseURL;
-
- @property (nonatomic, strong) AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer;
-
- @property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;
這是 AFURLSessionManager 的一個管理類,針對 HTTP 細化了不一樣類型的請求操做( GET、HEAD、POST、PUT、PATCH、DELETE ),由於 NSURLSession 是 iOS7 新增長的用於網絡請求相關的任務類,因此僅針對 iOS7 系統時可考慮優先使用這個管理類替代 AFHTTPRequestOperationManager ,若是須要考慮向前兼容,仍是須要使用 AFHTTPRequestOperationManager 。
連接地址9)AFURLRequestSerialization.h
這個文件主要定義了一些用於網絡請求的協議和類,其中包括了請求格式、請求參數以及相關請求設置的方法。
連接地址10)AFURLResponseSerialization.h
這個文件主要定義了一些網絡返回數據格式以及解析的協議和類,包括JSON、XML、Image等格式的返回數據獲取和格式解析等。
連接地址2. UIKit+AFNetworking
這是 AFNetworking 針對 UIKit 部分系統控件作的類別擴展,包括 1 個管理類定義和 8 個類別擴展。
連接地址1)UIKit+AFNetworking.h
- #import <UIKit/UIKit.h>
-
- #ifndef _UIKIT_AFNETWORKING_
- #define _UIKIT_AFNETWORKING_
-
- #import "AFNetworkActivityIndicatorManager.h"
-
- #import "UIActivityIndicatorView+AFNetworking.h"
- #import "UIAlertView+AFNetworking.h"
- #import "UIButton+AFNetworking.h"
- #import "UIImageView+AFNetworking.h"
- #import "UIKit+AFNetworking.h"
- #import "UIProgressView+AFNetworking.h"
- #import "UIWebView+AFNetworking.h"
- #endif /* _UIKIT_AFNETWORKING_ */
這是 UIKit+AFNetworking 的公共頭文件,若是須要使用 AFNetworking 的 UIKit 擴展時可直接在 Prefix.pch 文件中引入,或者在工程的相關文件中引入。
連接地址2)AFNetworkActivityIndicatorManager.h
- @interface AFNetworkActivityIndicatorManager : NSObject
-
- @property (nonatomic, assign, getter = isEnabled) BOOL enabled;
-
- @property (readonly, nonatomic, assign) BOOL isNetworkActivityIndicatorVisible;
這個類主要是爲了自動顯示和隱藏請求時的狀態提示,若是你確實須要它的話用這個類仍是很方便的,使用方法也很簡單。只要在 AppDelegate application:didFinishLaunchingWithOptions: 方法中添加一句
- [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
就能夠了,以後在使用 AFNetworking 發起請求和終止請求時都會自動顯示和隱藏狀態提示。
連接地址3)UIActivityIndicatorView+AFNetworking.h
- #import <Foundation/Foundation.h>
-
- #import <Availability.h>
-
- #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
-
- #import <UIKit/UIKit.h>
-
- @class AFURLConnectionOperation;
-
- @interface UIActivityIndicatorView (AFNetworking)
-
-
- #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
- - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task;
- #endif
-
-
- - (void)setAnimatingWithStateOfOperation:(AFURLConnectionOperation *)operation;
-
- @end
這個類別爲網絡請求的狀態顯示增長了兩個方法,經過這兩個方法能夠根據當前任務的狀態或操做的狀態決定網絡請求狀態的顯示與隱藏。
連接地址4)UIAlertView+AFNetworking.h
和上面的類別相似,不過這個類別主要是爲 UIAlertView 增長了幾個方法,當相關的網絡任務和請求操做發生錯誤時,會彈出一個 UIAlertView ,雖然 iOS7 的 UIAlertView 看上去溫柔不少,很我我的仍是很討厭這個粗暴的彈出提示,我一樣不喜歡轉圈圈的等待提示。
連接地址5)UIButton+AFNetworking.h
這個類別主要是爲 UIButton 增長了異步獲取網絡圖片的類別方法,用過相似 EGOImageView 的應該很容易理解。
連接地址6)UIImageView+AFNetworking.h
說曹操曹操到,這個就是 EGOImageView 的 AFNetworking 版。
連接地址7)UIProgressView+AFNetworking.h
同 UIActivityIndicatorView+AFNetworking ,只是這個類別是針對 UIProgressView 的。
連接地址8)UIRefreshControl+AFNetworking.h
同 UIActivityIndicatorView+AFNetworking ,只是這個類別是針對 UIRefreshControl 的。UIRefreshControl 是 iOS7 新增長的下拉刷新顯示控件,經過這個類別能夠根據網絡的行爲和請求結果決定 UIRefreshControl 的顯示狀態。
連接地址9)UIWebView+AFNetworking.h
爲 UIWebView 的載入請求增長了幾個類別方法,便於決定請求成功失敗如何顯示,以及請求過程當中等待狀態的顯示等。
連接地址3、 一點總結
粗略的瀏覽完 AFNetworking 的源代碼以後深入的感覺了一下那麼一句話:「咱們不生產代碼,咱們只是 Github 的搬運工!」。自省一下,繼續努力!
http://www.aiuxian.com/article/p-1715579.html