From 0624a938296983c7e03c0238f0959978384bc556 Mon Sep 17 00:00:00 2001 From: Bawn Date: Wed, 17 Feb 2016 13:09:12 +0800 Subject: [PATCH] fix #7 --- LCNetwork.podspec | 2 +- LCNetwork/LCBaseRequest.h | 8 ++++++++ LCNetwork/LCNetworkAgent.m | 11 +++++++++-- LCNetworkDemo/LCNetworkDemo/Api2.m | 2 -- LCNetworkDemo/Podfile | 1 - 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/LCNetwork.podspec b/LCNetwork.podspec index 51cdcfa..5185094 100644 --- a/LCNetwork.podspec +++ b/LCNetwork.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "LCNetwork" - s.version = "1.2.0" + s.version = "1.1.4" s.summary = "基于AFNetworking的网络库封装" s.homepage = "https://github.com/bawn/LCNetwork" s.license = 'MIT' diff --git a/LCNetwork/LCBaseRequest.h b/LCNetwork/LCBaseRequest.h index 99cd42d..3c237d0 100644 --- a/LCNetwork/LCBaseRequest.h +++ b/LCNetwork/LCBaseRequest.h @@ -144,6 +144,14 @@ typedef NS_ENUM(NSInteger , LCRequestSerializerType) { - (BOOL)removesKeysWithNullValues; +/** + * 添加请求 Header,比如返回@{@"Accept" : @"application/json"},那么 application/json 对应的是 Value,Accept对应的是 HTTPHeaderField + * + * @return NSDictionary + */ +- (NSDictionary *)requestHeaderValue; + + @end diff --git a/LCNetwork/LCNetworkAgent.m b/LCNetwork/LCNetworkAgent.m index f16fb94..ce93f73 100644 --- a/LCNetwork/LCNetworkAgent.m +++ b/LCNetwork/LCNetworkAgent.m @@ -77,7 +77,6 @@ - (void)addRequest:(LCBaseRequest *)request { serializer.removesKeysWithNullValues = [request.child removesKeysWithNullValues]; } self.manager.responseSerializer = serializer; - NSDictionary *argument = request.requestArgument; // 检查是否有统一的参数加工 if (self.config.processRule && [self.config.processRule respondsToSelector:@selector(processArgumentWithRequest:query:)]) { @@ -92,6 +91,14 @@ - (void)addRequest:(LCBaseRequest *)request { self.manager.requestSerializer = [AFJSONRequestSerializer serializer]; } } + if ([request.child respondsToSelector:@selector(requestHeaderValue)]) { + NSDictionary *headerValue = [request.child requestHeaderValue]; + if ([headerValue isKindOfClass:[NSDictionary class]]){ + [headerValue enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) { + [self.manager.requestSerializer setValue:obj forHTTPHeaderField:key]; + }]; + } + } if ([request.child requestMethod] == LCRequestMethodGet) { request.sessionDataTask = [self.manager GET:url parameters:argument progress:^(NSProgress * _Nonnull downloadProgress) { @@ -104,7 +111,7 @@ - (void)addRequest:(LCBaseRequest *)request { }]; } else if ([request.child requestMethod] == LCRequestMethodPost){ - // multipart `POST` request + // multipart POST request if ([request.child respondsToSelector:@selector(constructingBodyBlock)] && [request.child constructingBodyBlock]) { request.sessionDataTask = [self.manager POST:url parameters:argument constructingBodyWithBlock:[request.child constructingBodyBlock] progress:^(NSProgress * _Nonnull uploadProgress) { diff --git a/LCNetworkDemo/LCNetworkDemo/Api2.m b/LCNetworkDemo/LCNetworkDemo/Api2.m index 59dd447..2eb1cec 100644 --- a/LCNetworkDemo/LCNetworkDemo/Api2.m +++ b/LCNetworkDemo/LCNetworkDemo/Api2.m @@ -40,8 +40,6 @@ - (instancetype)initWith:(NSString *)lat lng:(NSString *)lng{ // }; //} - - - (BOOL)cacheResponse{ return YES; } diff --git a/LCNetworkDemo/Podfile b/LCNetworkDemo/Podfile index ee3d7f6..cbeff0b 100644 --- a/LCNetworkDemo/Podfile +++ b/LCNetworkDemo/Podfile @@ -3,6 +3,5 @@ platform:ios,'7.0' source 'https://github.com/CocoaPods/Specs.git' pod 'AFNetworking', '~> 3.0.2' -#pod 'AFNetworkActivityLogger' pod 'TMCache' pod 'MBProgressHUD' \ No newline at end of file