import ...">

AFNetWorking二次封装

作者: 疯狂de原始人 | 来源:发表于2016-12-19 15:01 被阅读120次

    "#import <Foundation/Foundation.h>

    import <UIKit/UIKit.h>

    typedef void (^HttpSuccessBlock)(id json);
    typedef void (^HttpFailureBlock)(NSError * error);
    typedef void (^HttpDownloadProgressBlock)(CGFloat progress);
    typedef void (^HttpUploadProgressBlock)(CGFloat progress);

    @interface HttpHandle : NSObject

    /**

    • get网络请求
    • @param path url地址
    • @param params url参数 NSDictionary类型
    • @param success 请求成功 返回NSDictionary或NSArray
    • @param failure 请求失败 返回NSError
      */
    • (void)getWithPath:(NSString *)path
      params:(NSDictionary *)params
      success:(HttpSuccessBlock)success
      failure:(HttpFailureBlock)failure;

    /**

    • post网络请求
    • @param path url地址
    • @param params url参数 NSDictionary类型
    • @param success 请求成功 返回NSDictionary或NSArray
    • @param failure 请求失败 返回NSError
      */
    • (void)postWithPath:(NSString *)path
      params:(NSDictionary *)params
      success:(HttpSuccessBlock)success
      failure:(HttpFailureBlock)failure;

    /**

    • 下载文件
    • @param path url路径
    • @param success 下载成功
    • @param failure 下载失败
    • @param progress 下载进度
      */
    • (void)downloadWithPath:(NSString *)path
      success:(HttpSuccessBlock)success
      failure:(HttpFailureBlock)failure
      progress:(HttpDownloadProgressBlock)progress;

    /**

    • 上传图片
    • @param path url地址
    • @param image UIImage对象
    • @param thumbName imagekey
    • @param params 上传参数
    • @param success 上传成功
    • @param failure 上传失败
    • @param progress 上传进度
      */
    • (void)uploadImageWithPath:(NSString *)path
      params:(NSDictionary *)params
      thumbName:(NSString *)imagekey
      image:(UIImage *)image
      success:(HttpSuccessBlock)success
      failure:(HttpFailureBlock)failure
      progress:(HttpUploadProgressBlock)progress;

    @end
    "
    "

    import "HttpHandle.h"

    import "AFNetworking.h"

    static NSString * kBaseUrl = SERVER_HOST;

    @interface AFHttpClient : AFHTTPSessionManager

    • (instancetype)sharedClient;

    @end

    @implementation AFHttpClient

    • (instancetype)sharedClient {

      static AFHttpClient * client = nil;
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{

        NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        
        client = [[AFHttpClient alloc] initWithBaseURL:[NSURL URLWithString:kBaseUrl] sessionConfiguration:configuration];
        //接收参数类型
        client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript",@"text/plain",@"image/gif", nil];
        //设置超时时间
        client.requestSerializer.timeoutInterval = 15;
        //安全策略
        client.securityPolicy = [AFSecurityPolicy defaultPolicy];
      

      });

      return client;
      }
      @end

    @implementation HttpHandle

    • (void)getWithPath:(NSString *)path
      params:(NSDictionary *)params
      success:(HttpSuccessBlock)success
      failure:(HttpFailureBlock)failure {
      //获取完整的url路径
      NSString * url = [kBaseUrl stringByAppendingPathComponent:path];

      [[AFHttpClient sharedClient] GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

        success(responseObject);
      

      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        failure(error);
      

      }];

    }

    • (void)postWithPath:(NSString *)path
      params:(NSDictionary *)params
      success:(HttpSuccessBlock)success
      failure:(HttpFailureBlock)failure {
      //获取完整的url路径
      NSString * url = [kBaseUrl stringByAppendingPathComponent:path];

      [[AFHttpClient sharedClient] POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

        success(responseObject);
      

      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        failure(error);
      

      }];

    }

    • (void)downloadWithPath:(NSString *)path
      success:(HttpSuccessBlock)success
      failure:(HttpFailureBlock)failure
      progress:(HttpDownloadProgressBlock)progress {

      //获取完整的url路径
      NSString * urlString = [kBaseUrl stringByAppendingPathComponent:path];

      //下载
      NSURL *URL = [NSURL URLWithString:urlString];

      NSURLRequest *request = [NSURLRequest requestWithURL:URL];

      NSURLSessionDownloadTask *downloadTask = [[AFHttpClient sharedClient] downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {

        progress(downloadProgress.fractionCompleted);
      

      } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

        //获取沙盒cache路径
        NSURL * documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
      

      } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

        if (error) {
            failure(error);
        } else {
            success(filePath.path);
        }
      

      }];

      [downloadTask resume];

    }

    • (void)uploadImageWithPath:(NSString *)path
      params:(NSDictionary *)params
      thumbName:(NSString *)imagekey
      image:(UIImage *)image
      success:(HttpSuccessBlock)success
      failure:(HttpFailureBlock)failure
      progress:(HttpUploadProgressBlock)progress {

      //获取完整的url路径
      NSString * urlString = [kBaseUrl stringByAppendingPathComponent:path];

      NSData * data = UIImagePNGRepresentation(image);

      [[AFHttpClient sharedClient] POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {

        [formData appendPartWithFileData:data name:imagekey fileName:@"01.png" mimeType:@"image/png"];
      

      } progress:^(NSProgress * _Nonnull uploadProgress) {

        progress(uploadProgress.fractionCompleted);
      

      } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

        success(responseObject);
      

      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        failure(error);
      

      }];
      }

    @end
    "

    相关文章

      网友评论

        本文标题:AFNetWorking二次封装

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