ios封装「别人的」学习封装封装

网络请求封装

2016-07-03  本文已影响143人  Evan叶少

有很多人面试时候都被问到,你是怎么封装网络请求的昵??

所以来聊聊,如何封装网络请求

////  HTTPTool.h//  HTTPNetworking////  Created by Evan on 16/7/3.//  Copyright © 2016年 Evan. All rights reserved.//

#importtypedef NS_ENUM(NSInteger, HTTPRequestType) { // 请求类型

HTTPRequestTypeGET = 0,

HTTPRequestTypeHEAD,

HTTPRequestTypePOST,

HTTPRequestTypePUT,

HTTPRequestTypePATCH,

HTTPRequestTypeDELETE,

};

// 使用Block来进行回调,看后台返回的数据进行回调

typedef void(^HTTPRequestSuccess)(NSURLSessionDataTask *task, id responseObject);// 成功回调

typedef void(^HTTPRequestFailure)(NSURLSessionDataTask *task, NSError *error);// 失败回调

typedef void(^HTTPRequestAllCompletion)();// 所有回调

@class HTTPTool;

@interface HTTPTool : NSObject

+ (HTTPTool *)defaultHTTPHelper;

/**

*  HTTP请求

*

*  @param type          请求类型

*  @param URLString     请求路径

*  @param parameters    请求参数

*  @param success       成功回调

*  @param failure       失败回调

*  @param allCompletion 完成回调

*/

- (NSURLSessionDataTask *)requestType:(HTTPRequestType)type URLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allcompletion:(HTTPRequestAllCompletion)allCompletion;

/**

*  GET请求

*

*  @param URLString     请求路径

*  @param parameters    请求参数

*  @param success       成功回调

*  @param failure       失败回调

*  @param allCompletion 完成回调

*/

- (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

/**

*  HEAD请求

*

*  @param URLString     请求路径

*  @param parameters    请求参数

*  @param success       成功回调

*  @param failure       失败回调

*  @param allCompletion 完成回调

*/

- (NSURLSessionDataTask *)HEAD:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

/**

*  POST请求

*

*  @param URLString     请求路径

*  @param parameters    请求参数

*  @param success       成功回调

*  @param failure       失败回调

*  @param allCompletion 完成回调

*/

- (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

/**

*  PUT请求

*

*  @param URLString     请求路径

*  @param parameters    请求参数

*  @param success       成功回调

*  @param failure       失败回调

*  @param allCompletion 完成回调

*/

- (NSURLSessionDataTask *)PUT:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

/**

*  PATCH请求

*

*  @param URLString     请求路径

*  @param parameters    请求参数

*  @param success       成功回调

*  @param failure       失败回调

*  @param allCompletion 完成回调

*/

- (NSURLSessionDataTask *)PATCH:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

/**

*  DELETE请求

*

*  @param URLString     请求路径

*  @param parameters    请求参数

*  @param success       成功回调

*  @param failure       失败回调

*  @param allCompletion 完成回调

*/

- (NSURLSessionDataTask *)DELETE:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

@end

实现网络请求方法

//

//  HTTPTool.m

//  HTTPNetworking

//

//  Created by Evan on 16/7/3.

//  Copyright © 2016年 Evan. All rights reserved.

//

#import "HTTPTool.h"

#import "AFNetworking.h"

@interface HTTPTool ()

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end

@implementation HTTPTool

- (instancetype)init

{

self = [super init];

if (self)

{

self.manager = [AFHTTPSessionManager manager];

self.manager.requestSerializer.timeoutInterval = 10;

self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"text/plain", @"application/json", @"text/json", @"text/javascript", @"text/html", @"image/png", nil];

self.manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

}

return self;

}

+ (HTTPTool *)defaultHTTPHelper;// 创建一个单例来管理

{

static HTTPTool *instances = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

instances = [[self alloc] init];

});

return instances;

}

- (NSURLSessionDataTask *)requestType:(HTTPRequestType)type URLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allcompletion:(HTTPRequestAllCompletion)allCompletion

{

switch (type) {// 判断是什么类型的网络请求,然后做出以下操作

case HTTPRequestTypeGET:

return [self GET:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

case HTTPRequestTypeHEAD:

return [self HEAD:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

case HTTPRequestTypePOST:

return [self POST:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

case HTTPRequestTypePUT:

return [self PUT:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

case HTTPRequestTypePATCH:

return [self PATCH:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

case HTTPRequestTypeDELETE:

return [self DELETE:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

default:

return nil;

break;

}

}

- (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

{

return  [self.manager GET:URLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

!success?:success(task,responseObject);// 这里使用三目运算来输出结果

!allCompletion?:allCompletion();

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

!failure?:failure(task,error);

!allCompletion?:allCompletion();

}];

}

- (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

{

return [self.manager POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

!success?:success(task,responseObject);

!allCompletion?:allCompletion();

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

!failure?:failure(task,error);

!allCompletion?:allCompletion();

}];

}

- (NSURLSessionDataTask *)PATCH:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

{

return [self.manager PATCH:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

!success?:success(task,responseObject);

!allCompletion?:allCompletion();

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

!failure?:failure(task,error);

!allCompletion?:allCompletion();

}];

}

- (NSURLSessionDataTask *)HEAD:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

{

return [self.manager HEAD:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task) {

!success?:success(task,nil);

!allCompletion?:allCompletion();

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

!failure?:failure(task,error);

!allCompletion?:allCompletion();

}];

}

- (NSURLSessionDataTask *)PUT:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

{

return [self.manager PUT:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

!success?:success(task,nil);

!allCompletion?:allCompletion();

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

!failure?:failure(task,error);

!allCompletion?:allCompletion();

}];

}

- (NSURLSessionDataTask *)DELETE:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

{

return [self.manager DELETE:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

!success?:success(task,nil);

!allCompletion?:allCompletion();

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

!failure?:failure(task,error);

!allCompletion?:allCompletion();

}];

}

@end

Demo:https://github.com/EvanYeShao/HTTPNetworking-

上一篇下一篇

猜你喜欢

热点阅读