轉:AFNetworking 與 UIKit+AFNetworking 詳解

資料來源 : 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

 

  1. #import <Foundation/Foundation.h>  
  2. #import <Availability.h>  
  3.   
  4. #ifndef _AFNETWORKING_  
  5.     #define _AFNETWORKING_  
  6.   
  7.     #import "AFURLRequestSerialization.h"  
  8.     #import "AFURLResponseSerialization.h"  
  9.     #import "AFSecurityPolicy.h"  
  10.     #import "AFNetworkReachabilityManager.h"  
  11.   
  12.     #import "AFURLConnectionOperation.h"  
  13.     #import "AFHTTPRequestOperation.h"  
  14.     #import "AFHTTPRequestOperationManager.h"  
  15.   
  16. #if ( ( defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090) || \  
  17.       ( defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 ) )  
  18.     #import "AFURLSessionManager.h"  
  19.     #import "AFHTTPSessionManager.h"  
  20. #endif  
  21.   
  22. #endif /* _AFNETWORKING_ */  

這是 AFNetworking 的公共頭文件,在使用 AFNetworking 庫時可直接在 Prefix.pch 文件中引入,或者在工程的網絡管理模塊相關文件中引入。 網絡

 

連接地址2)AFSecurityPolicy.h

 

  1. /** 
  2.  `AFSecurityPolicy` evaluates server trust against pinned X.509 certificates and public keys over secure connections. 
  3.   
  4.  Adding pinned SSL certificates to your app helps prevent man-in-the-middle attacks and other vulnerabilities. Applications dealing with sensitive customer data or financial information are strongly encouraged to route all communication over an HTTPS connection with SSL pinning configured and enabled. 
  5.  */  
  6. @interface AFSecurityPolicy : NSObject  
  7.   
  8. /** 
  9.  The criteria by which server trust should be evaluated against the pinned SSL certificates. Defaults to `AFSSLPinningModeNone`. 
  10.  */  
  11. @property (nonatomic, assign) AFSSLPinningMode SSLPinningMode;  
  12.   
  13. /** 
  14.  Whether to evaluate an entire SSL certificate chain, or just the leaf certificate. Defaults to `YES`. 
  15.  */  
  16. @property (nonatomic, assign) BOOL validatesCertificateChain;  
  17.   
  18. /** 
  19.  The certificates used to evaluate server trust according to the SSL pinning mode. By default, this property is set to any (`.cer`) certificates included in the app bundle. 
  20.  */  
  21. @property (nonatomic, strong) NSArray *pinnedCertificates;  
  22.   
  23. /** 
  24.  Whether or not to trust servers with an invalid or expired SSL certificates. Defaults to `NO`. 
  25.  */  
  26. @property (nonatomic, assign) BOOL allowInvalidCertificates;  
  27.   
  28. /** 
  29.  Whether or not to validate the domain name in the certificates CN field. Defaults to `YES` for `AFSSLPinningModePublicKey` or `AFSSLPinningModeCertificate`, otherwise `NO`. 
  30.  */  
  31. @property (nonatomic, assign) BOOL validatesDomainName;  

這個類主要是爲網絡請求添加 SSL 安全驗證, SSL 安全驗證類型有以下三種,默認是 AFSSLPinningModeNone 類型,另外經過 SSL 證書和密鑰能夠增長請求的安全性,避免請求被劫持和攻擊。session

 

 

  1. typedef NS_ENUM(NSUInteger, AFSSLPinningMode) {  
  2.     AFSSLPinningModeNone,  
  3.     AFSSLPinningModePublicKey,  
  4.     AFSSLPinningModeCertificate,  
  5. };  

 

關於 SSL 和數字證書相關可參考這裏(SSL)這裏(數字證書)。

連接地址3)AFNetworkReachabilityManager.h

 

  1. /** 
  2.  `AFNetworkReachabilityManager` monitors the reachability of domains, and addresses for both WWAN and WiFi network interfaces. 
  3.   
  4.  See Apple's Reachability Sample Code (https://developer.apple.com/library/ios/samplecode/reachability/) 
  5.   
  6.  @warning Instances of `AFNetworkReachabilityManager` must be started with `-startMonitoring` before reachability status can be determined. 
  7.  */  
  8. @interface AFNetworkReachabilityManager : NSObject  
  9.   
  10. /** 
  11.  The current network reachability status. 
  12.  */  
  13. @property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;  
  14.   
  15. /** 
  16.  Whether or not the network is currently reachable. 
  17.  */  
  18. @property (readonly, nonatomic, assign, getter = isReachable) BOOL reachable;  
  19.   
  20. /** 
  21.  Whether or not the network is currently reachable via WWAN. 
  22.  */  
  23. @property (readonly, nonatomic, assign, getter = isReachableViaWWAN) BOOL reachableViaWWAN;  
  24.   
  25. /** 
  26.  Whether or not the network is currently reachable via WiFi. 
  27.  */  
  28. @property (readonly, nonatomic, assign, getter = isReachableViaWiFi) BOOL reachableViaWiFi;  

 

這個類和蘋果官方提供的 Reachability 類功能相似,可是功能更增強大,不只增長了更多的公共屬性,也增長了狀態變動閉包(block)操做,還增長了通知標誌串,用過 Reachability 應該可以很快理解並愛上這個類。

連接地址4)AFURLConnectionOperation.h

 

  1. @interface AFURLConnectionOperation : NSOperation <NSURLConnectionDelegate, NSURLConnectionDataDelegate, NSCoding, NSCopying>  
  2.   
  3. ///-------------------------------  
  4. /// @name Accessing Run Loop Modes  
  5. ///-------------------------------  
  6.   
  7. /** 
  8.  The run loop modes in which the operation will run on the network thread. By default, this is a single-member set containing `NSRunLoopCommonModes`. 
  9.  */  
  10. @property (nonatomic, strong) NSSet *runLoopModes;  
  11.   
  12. ///-----------------------------------------  
  13. /// @name Getting URL Connection Information  
  14. ///-----------------------------------------  
  15.   
  16. /** 
  17.  The request used by the operation's connection. 
  18.  */  
  19. @property (readonly, nonatomic, strong) NSURLRequest *request;  
  20.   
  21. /** 
  22.  The last response received by the operation's connection. 
  23.  */  
  24. @property (readonly, nonatomic, strong) NSURLResponse *response;  
  25.   
  26. /** 
  27.  The error, if any, that occurred in the lifecycle of the request. 
  28.  */  
  29. @property (readonly, nonatomic, strong) NSError *error;  
  30.   
  31. ///----------------------------  
  32. /// @name Getting Response Data  
  33. ///----------------------------  
  34.   
  35. /** 
  36.  The data received during the request. 
  37.  */  
  38. @property (readonly, nonatomic, strong) NSData *responseData;  
  39.   
  40. /** 
  41.  The string representation of the response data. 
  42.  */  
  43. @property (readonly, nonatomic, copy) NSString *responseString;  
  44.   
  45. /** 
  46.  The string encoding of the response. 
  47.  
  48.  If the response does not specify a valid string encoding, `responseStringEncoding` will return `NSUTF8StringEncoding`. 
  49.  */  
  50. @property (readonly, nonatomic, assign) NSStringEncoding responseStringEncoding;  
  51.   
  52. ///-------------------------------  
  53. /// @name Managing URL Credentials  
  54. ///-------------------------------  
  55.   
  56. /** 
  57.  Whether the URL connection should consult the credential storage for authenticating the connection. `YES` by default. 
  58.  
  59.  This is the value that is returned in the `NSURLConnectionDelegate` method `-connectionShouldUseCredentialStorage:`. 
  60.  */  
  61. @property (nonatomic, assign) BOOL shouldUseCredentialStorage;  
  62.   
  63. /** 
  64.  The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`. 
  65.  
  66.  This will be overridden by any shared credentials that exist for the username or password of the request URL, if present. 
  67.  */  
  68. @property (nonatomic, strong) NSURLCredential *credential;  
  69.   
  70. ///-------------------------------  
  71. /// @name Managing Security Policy  
  72. ///-------------------------------  
  73.   
  74. /** 
  75.  The security policy used to evaluate server trust for secure connections. 
  76.  */  
  77. @property (nonatomic, strong) AFSecurityPolicy *securityPolicy;  
  78.   
  79. ///------------------------  
  80. /// @name Accessing Streams  
  81. ///------------------------  
  82.   
  83. /** 
  84.  The input stream used to read data to be sent during the request. 
  85.  
  86.  This property acts as a proxy to the `HTTPBodyStream` property of `request`. 
  87.  */  
  88. @property (nonatomic, strong) NSInputStream *inputStream;  
  89.   
  90. /** 
  91.  The output stream that is used to write data received until the request is finished. 
  92.  
  93.  By default, data is accumulated into a buffer that is stored into `responseData` upon completion of the request. When `outputStream` is set, the data will not be accumulated into an internal buffer, and as a result, the `responseData` property of the completed request will be `nil`. The output stream will be scheduled in the network thread runloop upon being set. 
  94.  */  
  95. @property (nonatomic, strong) NSOutputStream *outputStream;  
  96.   
  97. ///---------------------------------  
  98. /// @name Managing Callback Queues  
  99. ///---------------------------------  
  100.   
  101. /** 
  102.  The dispatch queue for `completionBlock`. If `NULL` (default), the main queue is used. 
  103.  */  
  104. @property (nonatomic, strong) dispatch_queue_t completionQueue;  
  105.   
  106. /** 
  107.  The dispatch group for `completionBlock`. If `NULL` (default), a private dispatch group is used. 
  108.  */  
  109. @property (nonatomic, strong) dispatch_group_t completionGroup;  
  110.   
  111. ///---------------------------------------------  
  112. /// @name Managing Request Operation Information  
  113. ///---------------------------------------------  
  114.   
  115. /** 
  116.  The user info dictionary for the receiver. 
  117.  */  
  118. @property (nonatomic, strong) NSDictionary *userInfo;  

這是一個 NSOperation 子類,它實現了 NSURLConnection 的所有代理方法,所執行的是單個網絡請求的操做。

 

連接地址5)AFHTTPRequestOperation.h

 

  1. /** 
  2.  `AFHTTPRequestOperation` is a subclass of `AFURLConnectionOperation` for requests using the HTTP or HTTPS protocols. It encapsulates the concept of acceptable status codes and content types, which determine the success or failure of a request. 
  3.  */  
  4. @interface AFHTTPRequestOperation : AFURLConnectionOperation  
  5.   
  6. ///------------------------------------------------  
  7. /// @name Getting HTTP URL Connection Information  
  8. ///------------------------------------------------  
  9.   
  10. /** 
  11.  The last HTTP response received by the operation's connection. 
  12.  */  
  13. @property (readonly, nonatomic, strong) NSHTTPURLResponse *response;  
  14.   
  15. /** 
  16.  Responses sent from the server in data tasks created with `dataTaskWithRequest:success:failure:` and run using the `GET` / `POST` / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to an AFHTTPResponse serializer, which uses the raw data as its response object. The serializer validates the status code to be in the `2XX` range, denoting success. If the response serializer generates an error in `-responseObjectForResponse:data:error:`, the `failure` callback of the session task or request operation will be executed; otherwise, the `success` callback will be executed. 
  17.  
  18.  @warning `responseSerializer` must not be `nil`. Setting a response serializer will clear out any cached value  
  19.  */  
  20. @property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;  
  21.   
  22. /** 
  23.  An object constructed by the `responseSerializer` from the response and response data. Returns `nil` unless the operation `isFinished`, has a `response`, and has `responseData` with non-zero content length. If an error occurs during serialization, `nil` will be returned, and the `error` property will be populated with the serialization error. 
  24.  */  
  25. @property (readonly, nonatomic, strong) id responseObject;  

這是 AFURLConnectionOperation 的子類,主要針對 HTTP 和 HTTPS 類型的請求,這也是最經常使用的請求操做。 

 

連接地址6)AFHTTPRequestOperationManager.h

 

  1. @interface AFHTTPRequestOperationManager : NSObject <NSCoding, NSCopying>  
  2.   
  3. /** 
  4.  The URL used to monitor reachability, and construct requests from relative paths in methods like `requestWithMethod:URLString:parameters:`, and the `GET` / `POST` / et al. convenience methods. 
  5.  */  
  6. @property (readonly, nonatomic, strong) NSURL *baseURL;  
  7.   
  8. /** 
  9.  Requests created with `requestWithMethod:URLString:parameters:` & `multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:` are constructed with a set of default headers using a parameter serialization specified by this property. By default, this is set to an instance of `AFHTTPRequestSerializer`, which serializes query string parameters for `GET`, `HEAD`, and `DELETE` requests, or otherwise URL-form-encodes HTTP message bodies. 
  10.   
  11.  @warning `requestSerializer` must not be `nil`. 
  12.  */  
  13. @property (nonatomic, strong) AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer;  
  14.   
  15. /** 
  16.  Responses sent from the server in data tasks created with `dataTaskWithRequest:success:failure:` and run using the `GET` / `POST` / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to a JSON serializer, which serializes data from responses with a `application/json` MIME type, and falls back to the raw data object. The serializer validates the status code to be in the `2XX` range, denoting success. If the response serializer generates an error in `-responseObjectForResponse:data:error:`, the `failure` callback of the session task or request operation will be executed; otherwise, the `success` callback will be executed. 
  17.  
  18.  @warning `responseSerializer` must not be `nil`. 
  19.  */  
  20. @property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;  
  21.   
  22. /** 
  23.  The operation queue on which request operations are scheduled and run. 
  24.  */  
  25. @property (nonatomic, strong) NSOperationQueue *operationQueue;  
  26.   
  27. ///-------------------------------  
  28. /// @name Managing URL Credentials  
  29. ///-------------------------------  
  30.   
  31. /** 
  32.  Whether request operations should consult the credential storage for authenticating the connection. `YES` by default. 
  33.  
  34.  @see AFURLConnectionOperation -shouldUseCredentialStorage 
  35.  */  
  36. @property (nonatomic, assign) BOOL shouldUseCredentialStorage;  
  37.   
  38. /** 
  39.  The credential used by request operations for authentication challenges. 
  40.  
  41.  @see AFURLConnectionOperation -credential 
  42.  */  
  43. @property (nonatomic, strong) NSURLCredential *credential;  
  44.   
  45. ///-------------------------------  
  46. /// @name Managing Security Policy  
  47. ///-------------------------------  
  48.   
  49. /** 
  50.  The security policy used by created request operations to evaluate server trust for secure connections. `AFHTTPRequestOperationManager` uses the `defaultPolicy` unless otherwise specified. 
  51.  */  
  52. @property (nonatomic, strong) AFSecurityPolicy *securityPolicy;  
  53.   
  54. ///------------------------------------  
  55. /// @name Managing Network Reachability  
  56. ///------------------------------------  
  57.   
  58. /** 
  59.  The network reachability manager. `AFHTTPRequestOperationManager` uses the `sharedManager` by default. 
  60.  */  
  61. @property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager;  

這是 AFHTTPRequestOperation 的一個管理類,細化了不一樣類型的請求操做( GET、HEAD、POST、PUT、PATCH、DELETE ),經過這個管理類建立的網絡請求操做都會被加入到 operationQueue 中執行。 

 

連接地址7)AFURLSessionManager.h

 

  1. @interface AFURLSessionManager : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSCoding, NSCopying>  
  2.   
  3. /** 
  4.  The managed session. 
  5.  */  
  6. @property (readonly, nonatomic, strong) NSURLSession *session;  
  7.   
  8. /** 
  9.  The operation queue on which delegate callbacks are run. 
  10.  */  
  11. @property (readonly, nonatomic, strong) NSOperationQueue *operationQueue;  
  12.   
  13. /** 
  14.  Responses sent from the server in data tasks created with `dataTaskWithRequest:success:failure:` and run using the `GET` / `POST` / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to an instance of `AFJSONResponseSerializer`. 
  15.  
  16.  @warning `responseSerializer` must not be `nil`. 
  17.  */  
  18. @property (nonatomic, strong) id <AFURLResponseSerialization> responseSerializer;  
  19.   
  20. ///-------------------------------  
  21. /// @name Managing Security Policy  
  22. ///-------------------------------  
  23.   
  24. /** 
  25.  The security policy used by created request operations to evaluate server trust for secure connections. `AFURLSessionManager` uses the `defaultPolicy` unless otherwise specified. 
  26.  */  
  27. @property (nonatomic, strong) AFSecurityPolicy *securityPolicy;  
  28.   
  29. ///--------------------------------------  
  30. /// @name Monitoring Network Reachability  
  31. ///--------------------------------------  
  32.   
  33. /** 
  34.  The network reachability manager. `AFURLSessionManager` uses the `sharedManager` by default. 
  35.  */  
  36. @property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager;  
  37.   
  38. ///----------------------------  
  39. /// @name Getting Session Tasks  
  40. ///----------------------------  
  41.   
  42. /** 
  43.  The data, upload, and download tasks currently run by the managed session. 
  44.  */  
  45. @property (readonly, nonatomic, strong) NSArray *tasks;  
  46.   
  47. /** 
  48.  The data tasks currently run by the managed session. 
  49.  */  
  50. @property (readonly, nonatomic, strong) NSArray *dataTasks;  
  51.   
  52. /** 
  53.  The upload tasks currently run by the managed session. 
  54.  */  
  55. @property (readonly, nonatomic, strong) NSArray *uploadTasks;  
  56.   
  57. /** 
  58.  The download tasks currently run by the managed session. 
  59.  */  
  60. @property (readonly, nonatomic, strong) NSArray *downloadTasks;  
  61.   
  62. ///---------------------------------  
  63. /// @name Managing Callback Queues  
  64. ///---------------------------------  
  65.   
  66. /** 
  67.  The dispatch queue for `completionBlock`. If `NULL` (default), the main queue is used. 
  68.  */  
  69. @property (nonatomic, strong) dispatch_queue_t completionQueue;  
  70.   
  71. /** 
  72.  The dispatch group for `completionBlock`. If `NULL` (default), a private dispatch group is used. 
  73.  */  
  74. @property (nonatomic, strong) dispatch_group_t completionGroup;  

這是 AFNetworking 實現的 NSURLSession 的一個管理類,在這個類裏面已經實現了所有相關的 NSURLSession 代理方法,NSURLSession 是 iOS7 新增長的用於網絡請求相關的任務類,具體可參考 這裏(蘋果官方文檔) 、 這裏(相關博客一) 和 這裏(相關博客二) 。 

 

連接地址8)AFHTTPSessionManager.h

 

  1. @interface AFHTTPSessionManager : AFURLSessionManager <NSCoding, NSCopying>  
  2.   
  3. /** 
  4.  The URL used to monitor reachability, and construct requests from relative paths in methods like `requestWithMethod:URLString:parameters:`, and the `GET` / `POST` / et al. convenience methods. 
  5.  */  
  6. @property (readonly, nonatomic, strong) NSURL *baseURL;  
  7.   
  8. /** 
  9.  Requests created with `requestWithMethod:URLString:parameters:` & `multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:` are constructed with a set of default headers using a parameter serialization specified by this property. By default, this is set to an instance of `AFHTTPRequestSerializer`, which serializes query string parameters for `GET`, `HEAD`, and `DELETE` requests, or otherwise URL-form-encodes HTTP message bodies. 
  10.   
  11.  @warning `requestSerializer` must not be `nil`. 
  12.  */  
  13. @property (nonatomic, strong) AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer;  
  14.   
  15. /** 
  16.  Responses sent from the server in data tasks created with `dataTaskWithRequest:success:failure:` and run using the `GET` / `POST` / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to an instance of `AFJSONResponseSerializer`. 
  17.  
  18.  @warning `responseSerializer` must not be `nil`. 
  19.  */  
  20. @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

 

  1. #import <UIKit/UIKit.h>  
  2.   
  3. #ifndef _UIKIT_AFNETWORKING_  
  4.     #define _UIKIT_AFNETWORKING_  
  5.   
  6.     #import "AFNetworkActivityIndicatorManager.h"  
  7.   
  8.     #import "UIActivityIndicatorView+AFNetworking.h"  
  9.     #import "UIAlertView+AFNetworking.h"  
  10.     #import "UIButton+AFNetworking.h"  
  11.     #import "UIImageView+AFNetworking.h"  
  12.     #import "UIKit+AFNetworking.h"  
  13.     #import "UIProgressView+AFNetworking.h"  
  14.     #import "UIWebView+AFNetworking.h"  
  15. #endif /* _UIKIT_AFNETWORKING_ */  

這是 UIKit+AFNetworking 的公共頭文件,若是須要使用 AFNetworking 的 UIKit 擴展時可直接在 Prefix.pch 文件中引入,或者在工程的相關文件中引入。 

 

連接地址2)AFNetworkActivityIndicatorManager.h

 

  1. @interface AFNetworkActivityIndicatorManager : NSObject  
  2.   
  3. /** 
  4.  A Boolean value indicating whether the manager is enabled. 
  5.  
  6.  If YES, the manager will change status bar network activity indicator according to network operation notifications it receives. The default value is NO. 
  7.  */  
  8. @property (nonatomic, assign, getter = isEnabled) BOOL enabled;  
  9.   
  10. /** 
  11.  A Boolean value indicating whether the network activity indicator is currently displayed in the status bar. 
  12.  */  
  13. @property (readonly, nonatomic, assign) BOOL isNetworkActivityIndicatorVisible;  

這個類主要是爲了自動顯示和隱藏請求時的狀態提示,若是你確實須要它的話用這個類仍是很方便的,使用方法也很簡單。只要在  AppDelegate application:didFinishLaunchingWithOptions: 方法中添加一句

 

 

  1. [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];   

 

就能夠了,以後在使用 AFNetworking 發起請求和終止請求時都會自動顯示和隱藏狀態提示。

連接地址3)UIActivityIndicatorView+AFNetworking.h

 

  1. #import <Foundation/Foundation.h>  
  2.   
  3. #import <Availability.h>  
  4.   
  5. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)  
  6.   
  7. #import <UIKit/UIKit.h>  
  8.   
  9. @class AFURLConnectionOperation;  
  10.   
  11. /** 
  12.  This category adds methods to the UIKit framework's `UIActivityIndicatorView` class. The methods in this category provide support for automatically starting and stopping animation depending on the loading state of a request operation or session task. 
  13.  */  
  14. @interface UIActivityIndicatorView (AFNetworking)  
  15.   
  16. ///----------------------------------  
  17. /// @name Animating for Session Tasks  
  18. ///----------------------------------  
  19.   
  20. /** 
  21.  Binds the animating state to the state of the specified task. 
  22.  
  23.  @param task The task. If `nil`, automatic updating from any previously specified operation will be disabled. 
  24.  */  
  25. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000  
  26. - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task;  
  27. #endif  
  28.   
  29. ///---------------------------------------  
  30. /// @name Animating for Request Operations  
  31. ///---------------------------------------  
  32.   
  33. /** 
  34.  Binds the animating state to the execution state of the specified operation. 
  35.   
  36.  @param operation The operation. If `nil`, automatic updating from any previously specified operation will be disabled. 
  37.  */  
  38. - (void)setAnimatingWithStateOfOperation:(AFURLConnectionOperation *)operation;  
  39.   
  40. @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

相關文章
相關標籤/搜索