苹果的规定:2016年6月1日提交到App Store必须支持IPv6-only网络。
升级AFNetworking2.X到最新版本 AFNetworking3.0
1、将 AFHTTPRequestOperationManager 替换为: AFHTTPSessionManager
2、将 AFHTTPRequestOperation 替换为:NSURLSessionTask
3、如果通过IP访问,或者允许https证书之外的域名访问,需要设置 validatesDomainName 为false。
#import <Foundation/Foundation.h>
typedef void (^requestBlock)(id message);
@interface HTTPRequestTool : NSObject
/**
* 封装AFNetworking工具方法
*/
+(void)sendGetAFRequest:(NSString *)url withParameters:(NSDictionary *)parDic withSuccess:(requestBlock)successMessage andWithFail:(requestBlock)failMessage;
+(void)sendPostAFRequest:(NSString *)url withParameters:(NSDictionary *)parDic withSuccess:(requestBlock)successMessage andWithFail:(requestBlock)failMessage;
@end
#import "HTTPRequestTool.h"
@implementation HTTPRequestTool
+(void)sendGetAFRequest:(NSString *)url withParameters:(NSDictionary *)parDic withSuccess:(requestBlock)successMessage andWithFail:(requestBlock)failMessage
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//设置AF框架响应的返回数据为2进制
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:parDic success:^(AFHTTPRequestOperation *operation, id responseObject)
{
//调用成功的block回调
if (successMessage)
{
successMessage(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//调用失败的block回调
if (failMessage)
{
failMessage(error);
}
}];
}
+ (void)sendPostAFRequest:(NSString *)url withParameters:(NSDictionary *)parDic withSuccess:(requestBlock)successMessage andWithFail:(requestBlock)failMessage {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//设置AF框架响应的返回数据为2进制
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:url parameters:parDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (successMessage) {
successMessage(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failMessage) {
failMessage(error);
}
}];
}
@end