美文网首页网络请求
AFNetWorking常用操作封装单例处理

AFNetWorking常用操作封装单例处理

作者: ITCooler | 来源:发表于2017-03-01 23:58 被阅读943次

    title: AFNetWorking常用操作封装单例处理
    date: 2017-03-01 07:39:00
    tags: AFNetworking 封装


    AFNetWorking作为最火的第三方开源网络请求库相信大家一点都不默生,不用像网络套接字编程一样要去处理一些底层的东西,它的使用就好像是我们直接在浏览器中调用一样,下面重点总结常用的方法。

    要点:

    1. 单例调用
    2. 增加图片上传和文件下载操作
    //
    //  AFNetWrokingAssistant.h
    //  FishShow
    //
    //  Created by Smith Cool on 2017/3/1.
    //  Copyright © 2017年 Smith Cool. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    #define ShareDefaultNetAssistant [AFNetWrokingAssistant shareAssistant]
    
    @interface AFNetWrokingAssistant : NSObject
    
    +(AFNetWrokingAssistant *)shareAssistant;
    //常用网络请求方法
    -(void)POSTWithCompleteURL:(NSString *)URLString
                    parameters:(id)parameters
                      progress:(void(^)(id progress))progress
                       success:(void (^)(id responseObject))success
                       failure:(void (^)(NSError *error))failure
                      useHTTPS:(BOOL)use;
    //常用网络请求方法
    -(void)POSTWithCompleteURL:(NSString *)URLString
                    parameters:(id)parameters
                      progress:(void(^)(id progress))progress
                       success:(void (^)(id responseObject))success
                       failure:(void (^)(NSError *error))failure;
    
    -(void)GETWithCompleteURL:(NSString *)URLString
                   parameters:(id)parameters
                     progress:(void(^)(id progress))progress
                      success:(void (^)(id responseObject))success
                      failure:(void (^)(NSError *error))failure;
    
    /**
     *  图片上传
     *
     *  @param imgArr 图片数组
     *  @param block  返回图片地址数组
     */
    - (void)uploadImagesWihtImgArr:(NSArray *)imgArr
                               url:(NSString *)url
                        parameters:(id)parameters
                             block:(void (^)(id objc,BOOL success))block;
    
    /**
     文件下载
     
     @param urlString 请求地址
     @param block 状态通知
     */
    - (void)downFileWithUrl:(NSString *)urlString
                      block:(void (^)(id objc,BOOL success))block;
    @end
    
    //
    //  AFNetWrokingAssistant.m
    //  FishShow
    //
    //  Created by Smith Cool on 2017/3/1.
    //  Copyright © 2017年 Smith Cool. All rights reserved.
    //
    
    #import "AFNetWrokingAssistant.h"
    
    #import "AFNetworking.h"
    
    #define ACCEPTTYPENORMAL @[@"application/json",@"application/xml",@"text/json",@"text/javascript",@"text/html",@"text/plain"]
    #define ACCEPTTYPEIMAGE @[@"text/plain", @"multipart/form-data", @"application/json", @"text/html", @"image/jpeg", @"image/png", @"application/octet-stream", @"text/json"]
    
    @interface AFNetWrokingAssistant()
    @property (strong,nonatomic)AFHTTPSessionManager *manager;
    @end
    
    @implementation AFNetWrokingAssistant
    
    +(AFNetWrokingAssistant *)shareAssistant{
        static dispatch_once_t onceToken;
        static AFNetWrokingAssistant *assistant = nil;
        if (assistant == nil) {
            dispatch_once(&onceToken, ^{
                assistant = [[AFNetWrokingAssistant alloc]init];
            });
        }
        return assistant;
    }
    
    -(instancetype)init{
        self = [super init];
        if (!self) {
            _manager = [AFHTTPSessionManager manager];
            _manager.requestSerializer = [AFJSONRequestSerializer serializer];
            [_manager.requestSerializer setTimeoutInterval:10];
            [_manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"User-Agent"];
        }
        return self;
    }
    
    //常用网络请求方法
    -(void)POSTWithCompleteURL:(NSString *)URLString
                    parameters:(id)parameters
                      progress:(void(^)(id progress))progress
                       success:(void (^)(id responseObject))success
                       failure:(void (^)(NSError *error))failure
                      useHTTPS:(BOOL)use{
        if (use) {
            [_manager setSecurityPolicy:[self customSecurityPolicy]];
        }
        [self POSTWithCompleteURL:URLString parameters:parameters progress:progress success:success failure:failure];
    }
    
    //常用网络请求方法
    -(void)POSTWithCompleteURL:(NSString *)URLString
                    parameters:(id)parameters
                      progress:(void(^)(id progress))progress
                       success:(void (^)(id responseObject))success
                       failure:(void (^)(NSError *error))failure{
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:ACCEPTTYPENORMAL];
        [_manager POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
    }
    
    -(void)GETWithCompleteURL:(NSString *)URLString
                   parameters:(id)parameters
                     progress:(void(^)(id progress))progress
                      success:(void (^)(id responseObject))success
                      failure:(void (^)(NSError *error))failure{
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:ACCEPTTYPENORMAL];
        [_manager GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
            progress(downloadProgress);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"success");
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
    }
    
    /**
     *  图片上传
     *
     *  @param imgArr 图片数组
     *  @param block  返回图片地址数组
     */
    - (void)uploadImagesWihtImgArr:(NSArray *)imgArr
                               url:(NSString *)url
                        parameters:(id)parameters
                             block:(void (^)(id objc,BOOL success))block{
        // 基于AFN3.0+ 封装的HTPPSession句柄
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:ACCEPTTYPEIMAGE];
        // 在parameters里存放照片以外的对象
        [_manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            for (int i = 0; i < imgArr.count; i++) {
                UIImage *image = imgArr[i];
                NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
                
                // 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
                // 要解决此问题,
                // 可以在上传时使用当前的系统事件作为文件名
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                // 设置时间格式
                [formatter setDateFormat:@"yyyyMMddHHmmss"];
                NSString *dateString = [formatter stringFromDate:[NSDate date]];
                NSString *fileName = [NSString  stringWithFormat:@"%@.jpg", dateString];
                /*
                 *该方法的参数
                 1. appendPartWithFileData:要上传的照片[二进制流]
                 2. name:对应网站上[upload.php中]处理文件的字段(比如upload)
                 3. fileName:要保存在服务器上的文件名
                 4. mimeType:上传的文件的类型
                 */
                [formData appendPartWithFileData:imageData name:@"multipartFile" fileName:fileName mimeType:@"image/jpeg"];
            }
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            NSLog(@"%@",uploadProgress);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            block(responseObject,YES);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            block(nil,NO);
        }];
    }
    
    
    /**
     文件下载
    
     @param urlString 请求地址
     @param block 状态通知
     */
    - (void)downFileWithUrl:(NSString *)urlString
                             block:(void (^)(id objc,BOOL success))block{
        // 2.设置请求的URL地址
        NSURL *url = [NSURL URLWithString:urlString];
        // 3.创建请求对象
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        [_manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
              NSLog(@"当前下载进度为:%lf", 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
                // 下载地址
                NSLog(@"默认下载地址%@",targetPath);
                // 设置下载路径,通过沙盒获取缓存地址,最后返回NSURL对象
                NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
                return [NSURL fileURLWithPath:filePath]; // 返回的是文件存放在本地沙盒的地址
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            // 下载完成调用的方法
            NSLog(@"%@---%@", response, filePath);
            block(filePath,YES);
        }];
    }
    
    //是否加入HTTPS证书
    -(AFSecurityPolicy *)customSecurityPolicy
    {
        //先导入证书,找到证书的路径
        NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"cerName" ofType:@"cer"];
        NSData *certData = [NSData dataWithContentsOfFile:cerPath];
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        securityPolicy.allowInvalidCertificates = YES;
        securityPolicy.validatesDomainName = NO;
        securityPolicy.pinnedCertificates = [NSSet setWithObjects:certData, nil];
        return securityPolicy;
    }
    
    -(void)cancelTask{
        [self.manager.operationQueue cancelAllOperations];
    }
    
    @end
    
    

    相关文章

      网友评论

      • timeforasong:需要取消所有的网络请求怎么封装在里面?
        这小子:-(void)cancelTask;写在.h里面,在什么地方取消,就在什么地方加方法.
      • 这小子:-(instancetype)init方法里为什么用if(!self),不应该是if(self)吗?
        这小子:@ITCooler 及时更新过来啊!
        ITCooler:是的,写错了
      • 童话镇里蜿蜒的河:请问楼主 AFNet会造成循环引用么? 内存泄漏呢?
        ITCooler:@童话镇里蜿蜒的河 我刚才通过instrument中的leaf检测对比了,如果每次调用网络请求接口的时候使用[AFHTTPSessionManager manager]会造成内存漏泄,这一步放到单例中就不会出现这个问题,你试试对比下呢
        童话镇里蜿蜒的河:@ITCooler 你确定你是将 AFHTTPSessionManager 设为了单利? 你确认下 ,因为我不是这样做的,我的有内存问题。我现在很苦恼
        ITCooler:不会啊,我使用的单例模式,只对AFHTTPSessionManager初始化一次,所以每次调用的网络请求都只能是我初始化的那一个对象

      本文标题:AFNetWorking常用操作封装单例处理

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