AFNetworking
ATS 设置
<key>NSAppTransportSecurity</key><dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/></dict>
封装网络框架的必要性
在应用程序开发中,一定要封装隔离网络框架
示意图如下:
OC 封装网络工具
新建网络工具类
NetworkTools
实现单例
+ (instancetype)sharedTools { static NetworkTools *instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[NetworkTools alloc] initWithBaseURL:nil];
instance.responseSerializer.acceptableContentTypes = [instance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; }); return instance; }
建立网络请求方法
- (void)request:(NSString *)URLString parameters:(id)parameters finished:(void (^)(id result, NSError *error))finished {
NSAssert(finished != nil, @"必须输入完成回调");
[self GET:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) { finished(responseObject, nil); } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) { finished(nil, error); }]; }
测试网络请求
[[NetworkTools sharedTools] request:@"http://www.weather.com.cn/data/sk/101010100.html" parameters:nil finished:^(id result, NSError *error) { NSLog(@"%@ %@", result, error); }];
定义 HTTP 请求枚举类型
/// HTTP 请求方法枚举typedef enum : NSUInteger { GET, POST, } HTTPRequestMethod;
从
AFHTTPSessionManager.m
中复制dataTaskWithHTTPMethod
函数定义@protocol NetworkToolsProxy
@optional- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;
@end
修改
request
方法- (void)request:(HTTPRequestMethod)method URLString:(NSString *)URLString parameters:(id)parameters finished:(void (^)(id result, NSError *error))finished {
NSAssert(finished != nil, @"必须输入完成回调"); NSString *methodName = (method == GET) ? @"GET" : @"POST";
[[self dataTaskWithHTTPMethod:methodName URLString:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) { finished(responseObject, nil); } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) { finished(nil, error); }] resume]; }
测试 GET 请求
[[NetworkTools sharedTools] request:GET URLString:@"http://www.weather.com.cn/data/sk/101010100.html" parameters:nil finished:^(id result, NSError *error) { NSLog(@"%@ %@", result, error); }];
测试 POST 请求
[[NetworkTools sharedTools] request:POST URLString:@"http://httpbin.org/post" parameters:@{@"name": @"zhangsan", @"age": @18} finished:^(id result, NSError *error) { NSLog(@"%@ %@", result, error); }];
Swift 封装网络工具
enum HTTPRequestMethod: String { case GET = "GET"
case POST = "POST"}class NetworkTools: AFHTTPSessionManager { // 单例
static let sharedTools: NetworkTools = { let instance = NetworkTools(baseURL: nil)
instance.responseSerializer.acceptableContentTypes?.insert("text/html") return instance
}() // 网络请求方法
func request(method: HTTPRequestMethod, URLString: String, parameters: AnyObject?, finished: (result: AnyObject?, error: NSError?)->()) { let success = { (task: NSURLSessionDataTask, result: AnyObject) -> Void in
finished(result: result, error: nil)
} let failure = { (task: NSURLSessionDataTask, error: NSError) -> Void in
print(error)
finished(result: nil, error: error)
} if method == .GET { GET(URLString, parameters: parameters, success: success, failure: failure)
} else { POST(URLString, parameters: parameters, success: success, failure: failure)
}
}
}