Commit f969a1bacfc941cc1921b1bf74933f66b8af65f5

Authored by zhangxiaowen
1 parent fa671759

no message

CNLivePlayer/Classes/DataRequest/CNLivePlayerNetworkManager.h
... ... @@ -24,25 +24,6 @@ typedef void(^GetVideoURLSuccessBlock)(NSString * _Nullable videoURL, NSInteger
24 24  
25 25 + (CNLivePlayerNetworkManager *)manager;
26 26  
27   -/**
28   - * GET 请求
29   - * @param URL url
30   - * @param parameters 上传参数
31   - * @param success 请求成功回调
32   - * @param failure 请求失败回调
33   - */
34   -+ (void)GET:(nullable NSString *)URL parameters:(nullable NSDictionary *)parameters success:(nullable void (^)(NSURLResponse *_Nullable response, id _Nullable responseObject))success failure:(nullable void (^)(NSURLResponse *_Nullable response, NSError *_Nullable error))failure;
35   -
36   -/**
37   - * POST 请求
38   - * @param URL url
39   - * @param parameters 上传参数
40   - * @param success 请求成功回调
41   - * @param failure 请求失败回调
42   - */
43   -+ (void)POST:(nullable NSString *)URL parameters:(nullable NSDictionary *)parameters success:(nullable void (^)(NSURLResponse *_Nullable response, id _Nullable responseObject))success failure:(nullable void (^)(NSURLResponse *_Nullable response, NSError *_Nullable error))failure;
44   -
45   -
46 27 #pragma mark - 针对具体业务
47 28 /**
48 29 * 鉴权
... ...
CNLivePlayer/Classes/DataRequest/CNLivePlayerNetworkManager.m
... ... @@ -26,7 +26,7 @@ static NSTimeInterval TimeoutInterval = 30.0 ;
26 26 */
27 27 static NSURLRequestCachePolicy CachePolicy = NSURLRequestUseProtocolCachePolicy;
28 28  
29   -@interface CNLivePlayerNetworkManager()<NSURLSessionDelegate>
  29 +@interface CNLivePlayerNetworkManager()
30 30 @property (nonatomic, assign) BOOL result;
31 31 @property (nonatomic, copy) GetVideoURLSuccessBlock successBlock;
32 32  
... ... @@ -54,27 +54,7 @@ static CNLivePlayerNetworkManager *_manager = nil;
54 54 return self;
55 55 }
56 56  
57   -+ (void)GET:(NSString *)URL parameters:(NSDictionary *)parameters success:(void (^)(NSURLResponse * _Nullable, id _Nullable))success failure:(void (^)(NSURLResponse * _Nullable, NSError * _Nullable))failure {
58   - [[CNLivePlayerNetworkManager manager] GET:URL parameters:parameters success:success failure:failure];
59   -
60   -}
61   -
62   -- (void)GET:(NSString *)URL parameters:(NSDictionary *)parameters success:(void (^)(NSURLResponse * _Nullable, id _Nullable))success failure:(void (^)(NSURLResponse * _Nullable, NSError * _Nullable))failure {
63   - NSURLSessionDataTask *task = [self dataTaskWithRequestMethod:@"GET" URLString:URL parameters:parameters success:success failure:failure];
64   - [task resume];
65   -}
66   -
67   -+ (void)POST:(NSString *)URL parameters:(NSDictionary *)parameters success:(void (^)(NSURLResponse * _Nullable, id _Nullable))success failure:(void (^)(NSURLResponse * _Nullable, NSError * _Nullable))failure {
68   - [[CNLivePlayerNetworkManager manager] POST:URL parameters:parameters success:success failure:failure];
69   -
70   -}
71   -
72   -- (void)POST:(NSString *)URL parameters:(NSDictionary *)parameters success:(void (^)(NSURLResponse * _Nullable, id _Nullable))success failure:(void (^)(NSURLResponse * _Nullable, NSError * _Nullable))failure {
73   - NSURLSessionDataTask *task = [self dataTaskWithRequestMethod:@"POST" URLString:URL parameters:parameters success:success failure:failure];
74   - [task resume];
75   -
76   -}
77   -
  57 +#pragma mark - 公共方法
78 58 - (NSURLSessionDataTask *)dataTaskWithRequestMethod:(NSString *)method URLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(NSURLResponse * _Nullable, id _Nullable))success failure:(void (^)(NSURLResponse * _Nullable, NSError * _Nullable))failure {
79 59  
80 60 if (![URLString isKindOfClass:[NSString class]]) {
... ... @@ -101,7 +81,7 @@ static CNLivePlayerNetworkManager *_manager = nil;
101 81  
102 82 if([method isEqual:@"POST"]){
103 83 [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
104   - request.HTTPBody =[[CNLiveURLRequestSerialization cn_QueryStringFromParameters:parameters] dataUsingEncoding:NSUTF8StringEncoding];
  84 + request.HTTPBody = [[CNLiveURLRequestSerialization cn_QueryStringFromParameters:parameters] dataUsingEncoding:NSUTF8StringEncoding];
105 85 }
106 86  
107 87 NSURLSessionDataTask *task = [self dataTaskWithRequest:request success:success failure:failure];
... ... @@ -140,30 +120,6 @@ static CNLivePlayerNetworkManager *_manager = nil;
140 120  
141 121 }
142 122  
143   -#pragma mark - NSURLSessionDelegate
144   -// 1.接收到服务器的响应
145   -- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
146   - // 允许处理服务器的响应,才会继续接收服务器返回的数据
147   - completionHandler(NSURLSessionResponseCancel);
148   - _manager.result = YES;
149   - NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
150   - [CNLivePlayerNetworkManager manager].successBlock([response.URL absoluteString], httpResponse.statusCode);
151   -
152   -}
153   -
154   -// 3.请求成功或者失败(如果失败,error有值)
155   -- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
156   - // 请求完成,成功或者失败的处理
157   - if (_manager.result) return;
158   -
159   - NSString *videoUrl302 = error.userInfo[@"NSErrorFailingURLStringKey"];
160   - if (!videoUrl302 || ![videoUrl302 isKindOfClass:[NSString class]] || videoUrl302.length <= 0) {
161   - videoUrl302 = nil;
162   - }
163   - [CNLivePlayerNetworkManager manager].successBlock(videoUrl302, error.code);
164   -
165   -}
166   -
167 123 #pragma mark - 针对具体业务
168 124 /**
169 125 * 鉴权
... ... @@ -178,15 +134,21 @@ static CNLivePlayerNetworkManager *_manager = nil;
178 134 * 获取视频url
179 135 */
180 136 - (void)getVideoURL:(nullable NSString *)URL success:(GetVideoURLSuccessBlock)success {
181   - _manager.successBlock = success;
182   - _manager.result = NO;
183   -
184   - NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
185   - NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:_manager delegateQueue:[[NSOperationQueue alloc] init]];
186   - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL] cachePolicy:_manager.cachePolicy timeoutInterval:_manager.timeoutInterval];
187   -
188   - NSURLSessionDataTask *task = [session dataTaskWithRequest:request];
189   - // 启动任务
  137 + NSURLSessionDataTask *task = [self dataTaskWithRequestMethod:@"GET" URLString:URL parameters:nil success:^(NSURLResponse * _Nullable response, id _Nullable responseObject) {
  138 + if(success){
  139 + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  140 + success([response.URL absoluteString], httpResponse.statusCode);
  141 + }
  142 +
  143 + } failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
  144 + if (success) {
  145 + NSString *videoUrl302 = error.userInfo[@"NSErrorFailingURLStringKey"];
  146 + if (!videoUrl302 || ![videoUrl302 isKindOfClass:[NSString class]] || videoUrl302.length <= 0) {
  147 + videoUrl302 = nil;
  148 + }
  149 + success(videoUrl302, error.code);
  150 + }
  151 + }];
190 152 [task resume];
191 153  
192 154 }
... ...