1.GET
+ (void)getRequestWithPath:(NSString *)pathString viewController:(UIViewController *)target success:(dataRequsetHelpersBlock)success {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//输出格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
//去除空值
((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;
[manager GET:pathString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
// 此处强制转换类型。
success(responseObject,response.statusCode);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
// 此处强制转换类型。
if (error.code == -1009) {
[AlertViewHelpers showViewAtBottomWithViewController:target string:@"网络请求失败"];
}
if (response.statusCode == 401) {
//token失效
// [NetRequestModel unauthoredAccessActionWith:target];
}
success(@"error",response.statusCode);
}];
}
2.DELETE
+ (void)deleteRequestWithPath:(NSString *)pathString viewController:(UIViewController *)target sucess:(dataRequsetHelpersBlock)success {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//去除空值
((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;
//输出格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
[manager DELETE:pathString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
// 此处强制转换类型。
success(responseObject,response.statusCode);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
if (target != nil) {
// 此处强制转换类型。
if (error.code == -1009) {
// [AlertViewHelpers setAlertViewWithMessage:@"网络请求失败" viewController:target];
[AlertViewHelpers showViewAtBottomWithViewController:target string:@"网络请求失败"];
}
}
success(@"error",response.statusCode);
}];
}
3.patch
+ (void)patchRequestWithParh:(NSString *)urlPath body:(NSDictionary *)bodyDic sucess:(dataRequsetHelpersBlock)success {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//去除空值
((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;
//输出格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
[manager PATCH:urlPath parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
// 此处强制转换类型。
success(responseObject,response.statusCode);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
// 此处强制转换类型。
success(@"error",response.statusCode);
}];
}
4.put
+ (void)putRequestWithParh:(NSString *)urlPath body:(NSDictionary *)bodyDic sucess:(dataRequsetHelpersBlock)success {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//去除空值
((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;
//输出格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
[manager PUT:urlPath parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
// 此处强制转换类型。
success(responseObject,response.statusCode);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
// 此处强制转换类型。
success(@"error",response.statusCode);
}];
}
5.post
+ (void)postRequestWithParh:(NSString *)urlPath body:(NSDictionary *)bodyDic sucess:(dataRequsetHelpersBlock)success {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//去除空值
((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;
//输出格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
[manager POST:urlPath parameters:bodyDic progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
// 此处强制转换类型。
success(responseObject,response.statusCode);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
// 此处强制转换类型。
success(@"error",response.statusCode);
}];
}
6.网络判断
+ (void)recachabilityStatus:(dataRequsetHelpersBlock)success {
// 检测网络连接状态
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
// 连接状态回调处理
/* AFNetworking的Block内使用self须改为weakSelf, 避免循环强引用, 无法释放 */
//__weak typeof(self) weakSelf = self;
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
{
switch (status)
{
case AFNetworkReachabilityStatusUnknown:
success(@"no",404);
// 回调处理
break;
case AFNetworkReachabilityStatusNotReachable:
success(@"no",404);
// 回调处理
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
success(@"wan",200);
// 回调处理
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
success(@"wifi",200);
// 回调处理
break;
default:
break;
}
}];
}
+ (void)cancelRequest {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.operationQueue cancelAllOperations];
}
oc_AF
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近比较清闲,就把以前学习的过程记录下吧,多少年后如果能在互联网上找到自己的痕迹,想想还是一件蛮值得高兴的事情。 ...
- OC中使用Swift 1、在项目中右键添加一个 Swift 文件,如TestSwift.swift。 2、添加后 ...
- 1、oc工程调用swift-----自动创建桥接头文件 创建一个oc工程: 设置Product Module Na...