现今一个app中网络库必不可少,而且大部分一般都会选择稳定第三方网络库,比如大名顶顶的AFNetworking性能好,稳定,大家用了都说好。之前公司用的基于libcurl写的网络库,因为libcurl是c++编写,所以可以实现ios与andorid跨平台使用,现在公司基于AF。所以一直很少关注ios提供的网络api。所以今天就聊聊封装一个基于NSURLSession的轻量级网络库。
封装一个post请求
- 定义回调
// 网络请求数据返回回调
typedef void (^WBHttpRequestSuccess)(id response);
typedef void (^WBHttpRequestFailure)(NSString *error);
// 调用时,自定义参数参入,避免出现大量重载函数,WBHttpRequestOrder是一个model,包涵请求需要的url,parameters等参数,可扩展
typedef void (^WBHttpRequest)(WBHttpRequestOrder *request);
- 定义请求方法
// WBNetWork.h
@interface WBNetWork : NSObject
+ (void)post:(WBHttpRequest)request success:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure;
@end
// WBNetWork.m
@implementation WBNetWork
// 从block中获取model
+ (id)setupRequest:(id)instance block:(WBHttpRequest)request {
if (request) {
request(instance);
}
return instance;
}
+ (void)post:(WBHttpRequest)request success:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure {
WBHttpRequestOrder *order = [[self class] setupRequest:[WBHttpRequestOrder new] block:request];
NSURL *url = [NSURL URLWithString:order.url];
id parameters = order.parameters;
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
mutableRequest.HTTPMethod = @"POST";
mutableRequest.HTTPBody = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:NULL];
[mutableRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:mutableRequest
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if ( error ) {
if ( failure ) failure([error description]);
return ;
}
if ( success ) {
NSString *reslut = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"reslut >> %@", reslut);
success(reslut);
}
}];
[task resume];
}
@end
- 测试调用
[WBNetWork post:^(WBHttpRequestOrder *request) {
request.url = @"http://www.jianshu.com/login";
request.parameters = @{@"username":@"wans", @"password":@"123"};
} success:^(id response) {
NSLog(@" seccess >> %@", response);
} failure:^(NSString *error) {
NSLog(@" failure >> %@", error);
}];
封装一个get请求
get请求与post请求类似,不同之处在于请求方法名不同和get请求没有body,parameters通过“&”拼接在url后面。
// 请求
url:http://www.jianshu.com/p/7c3256e26808
parameters:@{@"username":@"wans", @"password":@"123",@"info":@[@1,@"w",@"b"]}
// 转换结果
http://www.jianshu.com/p/7c3256e26808?username=wans&password=123&info[]=1&info[]=w&info[]=b
parameters转换成拼接字符串算法copy自AFNetWorking。
+ (void)get:(WBHttpRequest)request success:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure {
WBHttpRequestOrder *order = [self setupRequest:[WBHttpRequestOrder new] block:request];
NSURL *url = [NSURL URLWithString:order.url];
NSDictionary *parameters = order.parameters;
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
mutableRequest.HTTPMethod = @"GET";
NSString *queryString = AFQueryStringFromParameters(parameters);
if (queryString && queryString.length > 0) {
mutableRequest.URL = [NSURL URLWithString:[[mutableRequest.URL absoluteString] stringByAppendingFormat:mutableRequest.URL.query ? @"&%@" : @"?%@", queryString]];
if ( parameters.count > 0 ) {
[mutableRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
}
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:mutableRequest
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if ( error ) {
if ( failure ) failure([error description]);
return ;
}
if ( success ) {
NSString *reslut = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"reslut >> %@", reslut);
success(reslut);
}
}];
[task resume];
}
默认配置
定义WBNetWorkConfig勇于配置默认网络请求的默认信息,包括服务器地址,默认请求头,超时时常等。
// WBNetWork.m
static WBNetWorkConfig *httpConfig;
+ (WBNetWorkConfig *)httpConfig {
if ( !httpConfig ) {
httpConfig = [WBNetWorkConfig new];
}
return httpConfig;
}
+ (void)setHttpConfig:(WBNetWorkConfig *)config {
httpConfig = config;
}
程序启动时,初始化默认信息
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WBNetWork httpConfig].baseUrl = @"http://www.jianshu.com";
[WBNetWork httpConfig].timeoutInterval = 10;
[WBNetWork httpConfig].httpHeaderFields = [@{@"Accept":@"*/*;q=0.8",
@"Accept-Encoding":@"gzip, deflate, sdch",
@"Accept-Language":@"keep-alive",
@"User-Agent":@"WBNetworking/1.0 (iPhone; iOS 10.3; Scale/2.00)",
@"appChannel":@"App Store",
} mutableCopy];
}
优化&&解耦
将get与post方法合并,然后再做解耦处理。定义WBHTTPRequestManager来完成请求封装与执行操作。WBNetWork只负责调用。
// WBHTTPRequestManager.h
@interface WBHTTPRequestManager : NSObject
- (instancetype)initWithRequest:(WBHttpRequest)request method:(NSString *)method;
- (void)excuteTaskSuccess:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure;
@end
// WBHTTPRequestManager.m
- (instancetype)initWithRequest:(WBHttpRequest)request method:(NSString *)method {
self = [super init];
if ( self ) {
WBHttpRequestOrder *order = [self setupRequest:[WBHttpRequestOrder new] block:request];
self.URL = [NSURL URLWithString:order.url];
self.parameters = order.parameters;
self.method = method;
}
return self;
}
- (void)setRequest {
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:self.URL];
NSString *queryString = nil;
if ( [self.method isEqualToString:kWBHttpRequestMethodGet] ) {
queryString = AFQueryStringFromParameters(self.parameters);
if (queryString && queryString.length > 0) {
mutableRequest.URL = [NSURL URLWithString:[[mutableRequest.URL absoluteString] stringByAppendingFormat:mutableRequest.URL.query ? @"&%@" : @"?%@", queryString]];
if ( self.parameters.count > 0 ) {
[mutableRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
}
}
mutableRequest.HTTPMethod = self.method;
// 设置http header
NSMutableDictionary *fields = [WBNetWork httpConfig].httpHeaderFields;
for (NSString *field in fields.allKeys) {
[mutableRequest setValue:fields[field] forHTTPHeaderField:field];
}
// 设置超时时间
mutableRequest.timeoutInterval = [WBNetWork httpConfig].timeoutInterval;
self.mutableRequest = mutableRequest;
}
- (void)setBody {
if ( [self.method isEqualToString:kWBHttpRequestMethodPost] ) {
if (self.parameters) {
if (![self.mutableRequest valueForHTTPHeaderField:@"Content-Type"]) {
[self.mutableRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
}
NSError *error;
[self.mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:self.parameters options:NSJSONWritingPrettyPrinted error:&error]];
NSLog(@"json error: %@",[error description]);
}
}
}
- (void)setTask:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure {
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:self.mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if ( error ) {
if ( failure ) failure([error description]);
return ;
}
if ( success ) {
NSString *reslut = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
success(reslut);
}
}];
[task resume];
}
- (void)excuteTaskSuccess:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure {
[self setRequest];
[self setBody];
[self setTask:success failure:failure];
}
@end
// WBNetWork.h
@interface WBNetWork : NSObject
+ (WBNetWorkConfig *)httpConfig;
+ (void)setHttpConfig:(WBNetWorkConfig *)config;
+ (void)post:(WBHttpRequest)request success:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure;
+ (void)get:(WBHttpRequest)request success:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure;
@end
// WBNetWork.m
+ (void)post:(WBHttpRequest)request success:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure {
[[self self] request:request method:@"POST" success:success failure:failure];
}
+ (void)get:(WBHttpRequest)request success:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure {
[[self self] request:request method:@"GET" success:success failure:failure];
}
+ (void)request:(WBHttpRequest)request method:(NSString *)method success:(WBHttpRequestSuccess)success failure:(WBHttpRequestFailure)failure {
WBHTTPRequestManager *requestManager = [[WBHTTPRequestManager alloc] initWithRequest:request method:method];
[requestManager excuteTaskSuccess:success failure:failure];
}
以上,初步完成一个网络请求库的封装,demo,欢迎评论批评指正。