一、AFNetwork二次封裝方法一:網絡
#import <Foundation/Foundation.h> @interface BeeNetworkManager : NSObject /** * POST網絡請求 * 每一個請求都添加公共參數 */ + (void)POSTWithUrlString:(NSString *)urlString parameters:(NSMutableDictionary *)parameters success:(void (^)(id responseObject))success fail:(void (^)(NSError *error))fail; /** * GET網絡請求 */ + (void)GETWithUrlString:(NSString *)urlString parameters:(NSMutableDictionary *)parameters success:(void (^)(id responseObject))success fail:(void (^)(NSError *error))fail; @end
#import "BeeNetworkManager.h" #import "AFNetworking.h" @interface BeeNetworkManager () @end @implementation BeeNetworkManager + (void)POSTWithUrlString:(NSString *)urlString parameters:(NSMutableDictionary *)parameters success:(void (^)(id responseObject))success fail:(void (^)(NSError *error))fail { // SDK封裝 NSURLSessionConfiguration* configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; configuration.protocolClasses = @[[ICG_SDK protolClass]]; AFHTTPSessionManager* sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:nil sessionConfiguration:configuration]; [sessionManager.tasks makeObjectsPerformSelector:@selector(cancel)]; NSString *urlStr = [BASE_URL stringByAppendingString:urlString]; if (parameters == nil) { parameters = [NSMutableDictionary dictionary]; } // 添加公共參數 parameters = [self addPublicParametersWithDictionary:parameters]; [sessionManager POST:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { success(responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { fail(error); }]; } + (void)GETWithUrlString:(NSString *)urlString parameters:(NSMutableDictionary *)parameters success:(void (^)(id responseObject))success fail:(void (^)(NSError *error))fail { // SDK封裝 NSURLSessionConfiguration* configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; configuration.protocolClasses = @[[ICG_SDK protolClass]]; AFHTTPSessionManager* sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:nil sessionConfiguration:configuration]; sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer]; [sessionManager.tasks makeObjectsPerformSelector:@selector(cancel)]; NSString *urlStr = [BASE_URL stringByAppendingString:urlString]; if (parameters == nil) { parameters = [NSMutableDictionary dictionary]; } parameters = [self addPublicParametersWithDictionary:parameters]; [sessionManager GET:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { success(responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { fail(error); }]; } /*---------- 公共參數 ----------*/ + (NSMutableDictionary *)addPublicParametersWithDictionary:(NSMutableDictionary *)parameters { return parameters; } @end
二、AFNetwork二次封裝方法二:session
#import "AFHTTPSessionManager.h" @interface BeeCustomNetworkManager : AFHTTPSessionManager @end
#import "BeeCustomNetworkManager.h" @implementation BeeCustomNetworkManager + (instancetype)manager { return [[self alloc] initWithBaseURL:nil]; } - (instancetype)init { return [self initWithBaseURL:nil]; } - (instancetype)initWithBaseURL:(NSURL *)url { // SDK封裝 NSURLSessionConfiguration* configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; configuration.protocolClasses = @[[ICG_SDK protolClass]]; return [self initWithBaseURL:url sessionConfiguration:configuration]; } - (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters progress:(void (^)(NSProgress * _Nonnull))uploadProgress success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure { // 拼接URL NSString *urlStr = [BASE_URL stringByAppendingString:URLString]; // 設置公共參數 if (parameters == nil) { parameters = [NSMutableDictionary dictionary]; } parameters = [self addPublicParametersWithDictionary:parameters]; return [super POST:urlStr parameters:parameters progress:uploadProgress success:success failure:failure]; } - (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters progress:(void (^)(NSProgress * _Nonnull))downloadProgress success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure { // 拼接URL NSString *urlStr = [BASE_URL stringByAppendingString:URLString]; // 設置公共參數 if (parameters == nil) { parameters = [NSMutableDictionary dictionary]; } parameters = [self addPublicParametersWithDictionary:parameters]; return [super GET:urlStr parameters:parameters progress:downloadProgress success:success failure:failure]; } /*---------- 公共參數 ----------*/ - (NSMutableDictionary *)addPublicParametersWithDictionary:(NSMutableDictionary *)parameters { return parameters; }
三、網絡請求取消相關:url
取消請求:兩種取消請求方式spa
// 取消以前的全部請求,manager 還能夠發請求 [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
// 取消請求後,manager 不再可用發請求了 [self.manager invalidateSessionCancelingTasks:YES];