美文网首页
iOS网络请求工具类

iOS网络请求工具类

作者: fighter0501 | 来源:发表于2017-07-06 15:26 被阅读0次

    相信很多小伙伴都在为进行网络请求的数据处理之类的烦恼吧,下面就为小伙伴们分享自己开发过程中的网络请求工具类吧,直接上源码(这里的 JYLOG是自定义的输出,小伙伴们可以替换或者注释)

    + (void)networkRequestGET:(NSString *)url
                   parameters:(NSDictionary *)parameters
                      success:(void(^)(id object))success
                      failure:(void(^)(id error))failure{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
        
        [manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                
                responseObject = WWJSONObjectResetNullValue(responseObject, NSJSONReadingAllowFragments);
                
                
                NSDictionary *state = responseObject[kResponseState];
                NSInteger status = [state[kResponseStatus]integerValue];
                if(status == 0) {
                    JYLOG(@"--- 操作成功 --- :%@", responseObject);
                    success(responseObject[kResponseData]);
                } else {
                    JYLOG(@"--- 操作失败 --- :%@", responseObject);
                    failure(state[kResponseMessage]);
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(@"网络连接错误");
            JYLOG(@"%@", error);
        }];
    }
    
    + (void)networkRequestPOST:(NSString *)url
                                        parameters:(NSDictionary *)parameters
                                           success:(void(^)(id object))success
                                           failure:(void(^)(id error))failure
    {
        
        NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"https" ofType:@"cer"];
        NSData * certData =[NSData dataWithContentsOfFile:cerPath];
        NSSet * certSet = [[NSSet alloc] initWithObjects:certData, nil];
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
        
        [securityPolicy setAllowInvalidCertificates:YES];
        
        [securityPolicy setPinnedCertificates:certSet];
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
        
        NSLog(@"请求URL:%@\n请求参数:%@", url, parameters);
        [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                responseObject = WWJSONObjectResetNullValue(responseObject, NSJSONReadingAllowFragments);
                
                NSDictionary *state = responseObject[kResponseState];
                NSInteger status = [state[kResponseStatus]integerValue];
                if(status == 0) {
                    NSLog(@"--- 操作成功 --- :%@", responseObject);
                    success(responseObject[kResponseData]);
                } else {
                    NSLog(@"--- 操作失败 --- :%@", responseObject);
                    failure(state[kResponseMessage]);
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(@"网络连接错误");
            JYLOG(@"%@", error);
        }];
    }
    
    
    #pragma mark - Private
    
    static id WWJSONObjectResetNullValue(id JSONObject, NSJSONReadingOptions readingOptions) {
        if ([JSONObject isKindOfClass:[NSArray class]]) {
            NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:[(NSArray *)JSONObject count]];
            for (id value in (NSArray *)JSONObject) {
                [mutableArray addObject:WWJSONObjectResetNullValue(value, readingOptions)];
            }
    
            return (readingOptions & NSJSONReadingMutableContainers) ? mutableArray : [NSArray arrayWithArray:mutableArray];
        } else if ([JSONObject isKindOfClass:[NSDictionary class]]) {
            NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:JSONObject];
            for (id <NSCopying> key in [(NSDictionary *)JSONObject allKeys]) {
                id value = (NSDictionary *)JSONObject[key];
                if (!value || [value isEqual:[NSNull null]]) {
                    [mutableDictionary setObject:@"" forKey:key];
                } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
                    mutableDictionary[key] = WWJSONObjectResetNullValue(value, readingOptions);
                }
            }
    
            return (readingOptions & NSJSONReadingMutableContainers) ? mutableDictionary : [NSDictionary dictionaryWithDictionary:mutableDictionary];
        }
    
        return JSONObject;
    }
    

    只要使用以上方法就可以对你的接口进行包装请求数据了.

    相关文章

      网友评论

          本文标题:iOS网络请求工具类

          本文链接:https://www.haomeiwen.com/subject/oabhhxtx.html