①封装
创建一个新的类,我暂且命名为HttpRequest
.h文件
#import <Foundation/Foundation.h>
@class AFNetworking;
@interface HttpRequest : NSObject
+(HttpRequest *)sharedInstance;
-(void)GET:(NSString *)URLString dict:(id)dict succeed:(void (^)(id data))succeed failure:(void (^)(NSError *error))failure;
-(void)POST:(NSString *)URLString dict:(id)dict succeed:(void (^)(id data))succeed failure:(void (^)(NSError *error))failure;
@end
.m文件
#import "HttpRequest.h"
#import "AFNetworking.h"
@implementation HttpRequest
/**
* 创建网络请求类的单例
*/
static HttpRequest *httpRequest = nil;
+(HttpRequest *)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (httpRequest == nil) {
httpRequest = [[self alloc] init];
}
});
return httpRequest;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (httpRequest == nil) {
httpRequest = [super allocWithZone:zone];
}
});
return httpRequest;
}
-(instancetype)copyWithZone:(NSZone *)zone
{
return httpRequest;
}
/**
*封装AFN的GET请求
*@param URLString 网络请求地址
*@param dict 参数(可以是字典或者nil)
*@param succeed 成功后执行success block
*@param failure 失败后执行failure block
*/
- (void)GET:(NSString *)URLString dict:(id)dict succeed:(void (^)(id data))succeed failure:(void (^)(NSError *error))failure
{
//创建网络请求管理对象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//申明返回的结果是json类型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//如果报接受类型不一致请替换一致text/html或别的
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil nil];
//发送网络请求(请求方式为GET)
[manager GET:URLString parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
succeed(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
}
/**
* 封装AFN的POST请求
*
* @param URLString 网络请求地址
* @param dict 参数(可以是字典或者nil)
* @param succeed 成功后执行success block
* @param failure 失败后执行failure block
*/
- (void)POST:(NSString *)URLString dict:(id)dict succeed:(void (^)(id data))succeed failure:(void (^)(NSError *error))failure
{
//创建网络请求管理对象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//申明返回的结果是json类型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//如果报接受类型不一致请替换一致text/html或别的
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil nil];
//发送网络请求(请求方式为POST)
[manager POST:URLString parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
succeed(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
}
@end
②使用
调用时代码很少,很方便使用(也可以把调用的代码写成代码片段,提高效率)
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:<#(nonnull id), ...#>, nil nil];
HttpRequest *httpRequest = [HttpRequest sharedInstance];
[httpRequest GET:<#(NSString *)#> dict:dict succeed:^(id data) {
NSLog(@"%@", data);
} failure:^(NSError *error) {
NSLog(@"%@", error);
}];
如果不需要传参数,就不用创建dict了,get请求那里直接写nil就行.
post跟get一样,除了把get改成post,其余地方都不用改动