AFNetworking 结构
茹正初
2023-12-01
//以下说明为 AFNetworking版本 1.x
//For compatibility with iOS 5 or Mac OS X 10.7, use the latest 1.x release.
/**
一:AFNetworking 结构:
1: NSOperation--->AFURLConnectionOperation--->AFHTTPRequestOperation----->
包含如下几个:
AFImageRequestOperation;
AFJSONRequestOperation;
AFPropertyListRequestOperation;
AFXMLRequestOperation;
2: 以及一个可以管理的AFHTTPClient
3: 一些附属:
UIIMageView+AFNetworking 分类;
AFNetworkActivicatorManager 网络加载菊花指示器;
AFNetworking 引用头文件
二:详细情况:
1:AFURLConnectionOperation
包含几个 readonly的属性:request,response,error,responseData,responseString,responseStringEncoding,
可配置的:inputStream,outputStream
方法:
- (id)initWithRequest:(NSURLRequest *)urlRequest;
- (void)pause; //暂停
- (BOOL)isPaused;
- (void)resume; //恢复执行
- (void)setUploadProgressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block; //上传块
- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block; //下载块
2:AFHTTPRequestOperation
主要是一些HTTP头,状态码等的信息控制;
可配置:successCallbackQueue,failureCallbackQueue 类型为:dispatch_queue_t
+ (BOOL)canProcessRequest:(NSURLRequest *)urlRequest; 检查请求类型,如AFJSONRequestOperation 对应 json格式
设置请求完成块;
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
3:常用请求类型类:
------------------------------------------------------------------------------------
AFJSONRequestOperation
@property (readonly, nonatomic, strong) id responseJSON;
+ (instancetype)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure;
------------------------------------------------------------------------------------
AFImageRequestOperation
@property (readonly, nonatomic, strong) UIImage *responseImage;
@property (nonatomic, assign) CGFloat imageScale;
+ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(UIImage *image))success;
+ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
imageProcessingBlock:(UIImage *(^)(UIImage *image))imageProcessingBlock
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
------------------------------------------------------------------------------------
AFPropertyListRequestOperation
@property (readonly, nonatomic) id responsePropertyList;
@property (nonatomic, assign) NSPropertyListReadOptions propertyListReadOptions;
+ (instancetype)propertyListRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList))failure;
------------------------------------------------------------------------------------
AFXMLRequestOperation
@property (readonly, nonatomic, strong) NSXMLParser *responseXMLParser;
+ (instancetype)XMLParserRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser))failure;
4:辅助类:
AFNetworkActivityIndicatorManager
@property (nonatomic, assign, getter = isEnabled) BOOL enabled;
@property (readonly, nonatomic, assign) BOOL isNetworkActivityIndicatorVisible;
+ (instancetype)sharedManager;
UIImageView (AFNetworking)
- (void)setImageWithURL:(NSURL *)url;
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage;
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
- (void)cancelImageRequestOperation;
5: AFHTTPClient说明
@interface AFHTTPClient : NSObject <NSCoding, NSCopying>
@property (readonly, nonatomic, strong) NSURL *baseURL; 根url
@property (nonatomic, assign) NSStringEncoding stringEncoding; 编码
@property (readonly, nonatomic, strong) NSOperationQueue *operationQueue; 多线程管理池
@property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus; 网络连接状态
@property (nonatomic, assign) AFURLConnectionOperationSSLPinningMode defaultSSLPinningMode; SSL安全连接配置
+ (instancetype)clientWithBaseURL:(NSURL *)url;类方法初始化
- (id)initWithBaseURL:(NSURL *)url; 实例方法初始化
- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block; 网络连接类型变化块
- (BOOL)registerHTTPOperationClass:(Class)operationClass; 注册请求类型,比如上面的JSon,XML,Plist
- (void)unregisterHTTPOperationClass:(Class)operationClass;
- (void)setDefaultHeader:(NSString *)header 设置请求头
value:(NSString *)value;
- (void)setAuthorizationHeaderWithUsername:(NSString *)username
password:(NSString *)password; 用户名密码登陆
- (void)setAuthorizationHeaderWithToken:(NSString *)token; 登陆带token
//form请求配置
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters;
- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block;
常用请求
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
把请求放到队列里;
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;
批量请求放到队列里;
- (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;
批量添加队列请求操作
- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;
一些基本用的get ,post,等rest 操作
- (void)getPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
- (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
- (void)deletePath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
//补丁请求‘
- (void)patchPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
*/
//操作示例:
#pragma mark json,xml,plist,数据解析
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
//json
NSString *ipJson = @"http://ip-api.com/json";
NSURLRequest *jsonRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:ipJson]];
AFJSONRequestOperation *jsonOp = [AFJSONRequestOperation JSONRequestOperationWithRequest:jsonRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"json Data %@",JSON);
NSLog(@"------------------");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"false %@",error);
}];
[jsonOp start];
NSLog(@"isfinised %d",[jsonOp isFinished]);
//xml
NSURL *ipXML = [NSURL URLWithString:kIP_XML];
AFXMLRequestOperation *xmlOp = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:[NSURLRequest requestWithURL:ipXML] success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
NSLog(@"xml Data %@",XMLParser);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
NSLog(@"false %@",error);
}];
[xmlOp start];
//Plist
NSURLRequest *plistRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:kPlistDataApi]];
AFPropertyListRequestOperation *plistOp = [AFPropertyListRequestOperation propertyListRequestOperationWithRequest:plistRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList) {
NSLog(@"plist data %@",propertyList);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList) {
NSLog(@"false %@",error);
}];
[plistOp start];