AFNetWorking 2차 패키지

6407 단어
"#import

import


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 경로
  • @paramsuccess 다운로드 성공
  • @paramfailure 다운로드 실패
  • @paramprogress 다운로드 진도*/
  • (void)downloadWithPath:(NSString *)path success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure progress:(HttpDownloadProgressBlock)progress;

  • /**
  • 사진 업로드
  • @param path url 주소
  • @param image UIImage 객체
  • @param thumbName imagekey
  • @paramparams 업로드 매개 변수
  • @paramsuccess 업로드 성공
  • @paramfailure 업로드 실패
  • @paramprogress 업로드 진도*/
  • (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 stringByAppending PathComponent: 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: (Http SuccessBlock) success failure: (Http Failure Block) failure {//완전한 url 경로 가져오기 NSString* url = [kBaseUrl stringByAppending PathComponent: path]; [[AFHttp Client shared Client] POST: url parameters: params progress s:nil success:^ (NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
      success(responseObject);
    
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
      failure(error);
    
    }];

  • }
  • (void) downloadWithPath: (NSString *) path success: (Http Success Block) success failure: (Http Failure Block) failure progress: (Http Download Progress Block) progress {//완전한 url 경로 가져오기 NSString * urlString = [kBaseUrl stringBy Appending PathComponent: path];/다운로드 NSURL * URL = [NSURL WithString: url String] ring];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: (Http SuccessBlock) success failure: (Http Failure Block) failure progress: (Http Upload Progress Block) progress {//전체 url 경로 가져오기 NSString* urlString = [kBaseUrl stringByAppendingPathComponent:path]; NSData * data = UIImagePNGrepresentation(image); [[AFHttpClient sharedClient] POST:urlString parameters:params constructingBodyWithBlock:^(id _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 "

    좋은 웹페이지 즐겨찾기