HttpTool.h HttpTool.m ...
HttpTool.h
#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 HttpTool : 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 *)thumbName image:(UIImage *)image success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure progress:(HttpUploadProgressBlock)progress; @end
HttpTool.m
#import "HttpTool.h" #import "AFNetworking/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/plain", @"text/gif", nil]; // 設置超時時間,預設60 client.requestSerializer.timeoutInterval = 15; // 安全策略 client.securityPolicy = [AFSecurityPolicy defaultPolicy]; }); return client; } @end @implementation HttpTool + (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 *url = [kBaseUrl stringByAppendingPathComponent:path]; // 下載 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; NSURLSessionDownloadTask *downloadTask = [[AFHttpClient sharedClient] downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { progress(downloadProgress.fractionCompleted); } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { // 獲取沙盒cache路徑 NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { if (error) { failure(error); } else { success(filePath.path); } }]; [downloadTask resume]; } + (void)uploadImageWithPath:(NSString *)path params:(NSDictionary *)params thumbName:(NSString *)thumbName image:(UIImage *)image success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure progress:(HttpUploadProgressBlock)progress { // 獲取完整的url路徑 NSString *url = [kBaseUrl stringByAppendingPathComponent:path]; NSData *data = UIImagePNGRepresentation(image); [[AFHttpClient sharedClient] POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { [formData appendPartWithFileData:data name:thumbName 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