首页 > 代码库 > iOS基础之网络请求相关
iOS基础之网络请求相关
1、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
2、AFNetwork二次封装方法二:
#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; }
3、网络请求取消相关:
取消请求:两种取消请求方式
// 取消之前的所有请求,manager 还可以发请求 [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
// 取消请求后,manager 再也不可用发请求了 [self.manager invalidateSessionCancelingTasks:YES];
iOS基础之网络请求相关
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。