美文网首页
基于AFN3.X 的网路请求库二次封装

基于AFN3.X 的网路请求库二次封装

作者: YannChee | 来源:发表于2017-03-06 18:47 被阅读93次

    为尽量保证AFN的原汁原味和实用性,但又不至于让项目对第三方框架的过渡依赖,对AFN进行了二次封装

    1.创建一个继承自NSObject的类,在 .h 文件中提供 get,post,upload,cancel四种方法

    @class QYUploadFile;
    @interface QYHttpTool : NSObject
    /// get
    + (void)GET:(NSString *)urlString
     parameters:(id)parameters
       progress:(void (^)(NSProgress* downloadProgress))downloadProgress
        success:(void (^)(id responseObject))success
        failure:(void (^)(NSError* error))failure;
    
    /// post
    + (void)POST:(NSString *)urlString
      parameters:(id)parameters
        progress:(void (^)(NSProgress *uploadProgress))uploadProgress
         success:(void (^)(id responseObject))success
         failure:(void (^)(NSError* error))failure;
    
    /// upload
    + (void)upload:(NSString *)urlString
        parameters:(id)parameters
        uploadFile:(QYUploadFile* )uploadFile
          progress:(void (^)(NSProgress* uploadProgress))progress
           success:(void (^)(id responseObject))success
           failure:(void (^)(NSError* error))failure;
    
    /// 取消发出请求后对回调block的操作
    + (void)cancelAllOperations;
    

    在.m文件具体实现

    
    + (void)GET:(NSString *)urlString
            parameters:(id)parameters
            progress:(void (^)(NSProgress* downloadProgress))progress
            success:(void (^)(id responseObject))success
            failure:(void (^)(NSError* error))failure{
        
        QYHttpSessionManager*  manager = [QYHttpSessionManager sharedManager];
        
        [manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
            if (progress) {
                progress(downloadProgress);
            }
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (success) {
                success(responseObject);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                failure(error);
            }
        }];
        
    }
    
    + (void)POST:(NSString *)urlString
      parameters:(id)parameters
        progress:(void (^)(NSProgress *uploadProgress))progress
         success:(void (^)(id responseObject))success
         failure:(void (^)(NSError* error))failure{
        
        QYHttpSessionManager*  manager = [QYHttpSessionManager sharedManager];
    
        [manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
            if (progress) {
                progress(uploadProgress);
            }
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (success) {
                success(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                failure(error);
            }
        }];
    
    }
    
    + (void)upload:(NSString *)urlString
           parameters:(id)parameters
           uploadFile:(QYUploadFile* )uploadFile
          progress:(void (^)(NSProgress* uploadProgress))progress
           success:(void (^)(id responseObject))success
           failure:(void (^)(NSError* error))failure{
        
        QYHttpSessionManager*  manager = [QYHttpSessionManager sharedManager];
    
        
        [manager POST:urlString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
            
            /**
              上传的文件全部拼接到formData
             *
             *  FileData:要上传的文件的二进制数据
             *  name:上传参数名称
             *  fileName:上传到服务器的文件名称
             *  mimeType:文件类型
             */
            [formData appendPartWithFileData:uploadFile.data name:uploadFile.paramName fileName:uploadFile.fileName mimeType:uploadFile.mimeType];
    
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            if (progress) {
                progress(uploadProgress);
            }
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (success) {
                success(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                failure(error);
            }
        }];
    
    }
    
    + (void)cancelAllOperations {
        [[QYHttpSessionManager sharedManager].operationQueue cancelAllOperations];
    }
    
    
    

    2.创建一个继承自AFHTTPSessionManager类 QYHttpSessionManager,设置为单例

    //单例方法
    +(instancetype)sharedManager {
        
        static QYHttpSessionManager *manager;
        
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            manager = [QYHttpSessionManager manager];
            // 其它配置设置
            manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
        
        });
        
        return manager;
    }
    

    3.为防止工具类中上传文件方法对AFMultipartFormData的依赖,创建一个模型类QYUploadFile存储上传文件信息,还可以根据 formData的需要向该类增加新的属性

    /**
     *  上传文件的二进制数据
     */
    @property (nonatomic, strong) NSData *data;
    /**
     *  上传的参数名称
     */
    @property (nonatomic, copy) NSString* paramName;
    /**
     *  上传到服务器后的文件名称
     */
    @property (nonatomic, copy) NSString *fileName;
    
    /**
     *  上传文件的类型
     */
    @property (nonatomic, copy) NSString *mimeType;
    

    相关文章

      网友评论

          本文标题:基于AFN3.X 的网路请求库二次封装

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