本文主要是介绍iOS开发总结之block回调以AFN为基础封装的网络请求操作类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.#warning封装网络请求类的好处
/**
* 1.项目存在问题,太依赖第三方框架。
2.为什么要封装网络请求:以后AFN升级,方法名改了,或者AFN淘汰了,直接改工具类就好了。
3.好处:解耦,不依赖第三方框架
*/
2.代码
#import"TingHttpTool.h"
#import "Reachability.h"
#import "AFNetworking.h"
#import "MUpdate.h"
#import "Define.h"
@implementation TingHttpTool+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{// 1.创建请求管理者AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];mgr.requestSerializer.timeoutInterval = 10;// 2.发送请求[mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {if (success) {success(responseObject);}} failure:^(AFHTTPRequestOperation *operation, NSError *error) {if (failure) {failure(error);}}];
}+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{// 1.创建请求管理者AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];mgr.requestSerializer.timeoutInterval = 10;// 2.发送请求[mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {if (success) {success(responseObject);}} failure:^(AFHTTPRequestOperation *operation, NSError *error) {if (failure) {failure(error);}}];
}
3.使用:
[TingHttpTool post:urladr params:dic success:^(id json) {} failure:^(NSError *error) {}];
这篇关于iOS开发总结之block回调以AFN为基础封装的网络请求操作类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!