.打开下载下来的demo,运行如下(其中红色箭头所指的文件是我们拖到项目中的两个文件):
1678515-d4b859838f96a269.jpg
2.将拖到新建的项目中,在info.plist文件中添加ATS(App Transporty security……),command+B编译一下,发现没有问题即可。ATS截图如下:
1678515-491d06a5ac337984.jpg
到目前来说,AFN3.0已经集成完毕,但是有些细心的朋友会发现,AFN3.0的工程中出现了好多SSL证书,如图所示:
1678515-8c1a0bd2a9d03cfe.jpg
这种情况下:自己也爬网查了一些东西,提供两个网址以供参考:
http://www.cnblogs.com/jys509/p/5001566.html
http://oncenote.com/2014/10/21/Security-1-HTTPS/
讲的很专业,希望对读到的朋友有帮助!
二次封装
主要是新建了一个NSObject的子类,对AFN的功能进行了集成:
LHAFNetWork.h
#import#import"AFNetworking/AFNetworking.h"/**
* 宏定义请求成功的block
*
* @param response 请求成功返回的数据
*/typedefvoid(^LHResponseSuccess)(NSURLSessionDataTask* task,idresponseObject);/**
* 宏定义请求失败的block
*
* @param error 报错信息
*/typedefvoid(^LHResponseFail)(NSURLSessionDataTask* task,NSError* error);/**
* 上传或者下载的进度
*
* @param progress 进度
*/typedefvoid(^LHProgress)(NSProgress*progress);@interfaceLHAFNetWork:NSObject/**
* 普通get方法请求网络数据
*
* @param url 请求网址路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/+(void)GET:(NSString*)urlparams:(NSDictionary*)params success:(LHResponseSuccess)success fail:(LHResponseFail)fail;/**
* 含有baseURL的get方法
*
* @param url 请求网址路径
* @param baseUrl 请求网址根路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/+(void)GET:(NSString*)url baseURL:(NSString*)baseUrlparams:(NSDictionary*)params success:(LHResponseSuccess)success fail:(LHResponseFail)fail;/**
* 普通post方法请求网络数据
*
* @param url 请求网址路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/+(void)POST:(NSString*)url params:(NSDictionary*)paramssuccess:(LHResponseSuccess)success fail:(LHResponseFail)fail;/**
* 含有baseURL的post方法
*
* @param url 请求网址路径
* @param baseUrl 请求网址根路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/+(void)POST:(NSString*)urlbaseURL:(NSString*)baseUrl params:(NSDictionary*)paramssuccess:(LHResponseSuccess)success fail:(LHResponseFail)fail;/**
* 普通路径上传文件
*
* @param url 请求网址路径
* @param params 请求参数
* @param filedata 文件
* @param name 指定参数名
* @param filename 文件名(要有后缀名)
* @param mimeType 文件类型
* @param progress 上传进度
* @param success 成功回调
* @param fail 失败回调
*/+(void)uploadWithURL:(NSString*)url params:(NSDictionary*)params fileData:(NSData*)filedata name:(NSString*)name fileName:(NSString*)filename mimeType:(NSString*) mimeType progress:(LHProgress)progress success:(LHResponseSuccess)success fail:(LHResponseFail)fail;/**
* 含有跟路径的上传文件
*
* @param url 请求网址路径
* @param baseurl 请求网址根路径
* @param params 请求参数
* @param filedata 文件
* @param name 指定参数名
* @param filename 文件名(要有后缀名)
* @param mimeType 文件类型
* @param progress 上传进度
* @param success 成功回调
* @param fail 失败回调
*/+(void)uploadWithURL:(NSString*)url baseURL:(NSString*)baseurl params:(NSDictionary*)params fileData:(NSData*)filedata name:(NSString*)name fileName:(NSString*)filename mimeType:(NSString*) mimeType progress:(LHProgress)progress success:(LHResponseSuccess)success fail:(LHResponseFail)fail;/**
* 下载文件
*
* @param url 请求网络路径
* @param fileURL 保存文件url
* @param progress 下载进度
* @param success 成功回调
* @param fail 失败回调
*
* @return 返回NSURLSessionDownloadTask实例,可用于暂停继续,暂停调用suspend方法,重新开启下载调用resume方法
*/+(NSURLSessionDownloadTask*)downloadWithURL:(NSString*)url savePathURL:(NSURL*)fileURL progress:(LHProgress )progress success:(void(^)(NSURLResponse*,NSURL*))success fail:(void(^)(NSError*))fail;@end
LHAFNetWork.m
#import"LHAFNetWork.h"@implementationLHAFNetWork+(void)GET:(NSString*)url params:(NSDictionary*)params success:(LHResponseSuccess)success fail:(LHResponseFail)fail{AFHTTPSessionManager *manager = [LHAFNetWork managerWithBaseURL:nilsessionConfiguration:NO];[manager GET:url parameters:params progress:nilsuccess:^(NSURLSessionDataTask* _Nonnull task,id_Nullable responseObject) {iddic = [LHAFNetWork responseConfiguration:responseObject]; success(task,dic);} failure:^(NSURLSessionDataTask* _Nullable task,NSError* _Nonnull error) { fail(task,error);}];}+(void)GET:(NSString*)url baseURL:(NSString*)baseUrl params:(NSDictionary*)params success:(LHResponseSuccess)success fail:(LHResponseFail)fail{AFHTTPSessionManager *manager = [LHAFNetWork managerWithBaseURL:baseUrl sessionConfiguration:NO];[manager GET:url parameters:params progress:^(NSProgress* _Nonnull downloadProgress) {} success:^(NSURLSessionDataTask* _Nonnull task,id_Nullable responseObject) {iddic = [LHAFNetWork responseConfiguration:responseObject]; success(task,dic);} failure:^(NSURLSessionDataTask* _Nullable task,NSError* _Nonnull error) { fail(task,error);}];}+(void)POST:(NSString*)url params:(NSDictionary*)paramssuccess:(LHResponseSuccess)success fail:(LHResponseFail)fail{AFHTTPSessionManager *manager = [LHAFNetWork managerWithBaseURL:nilsessionConfiguration:NO];[manager POST:url parameters:params progress:nilsuccess:^(NSURLSessionDataTask* _Nonnull task,id_Nullable responseObject) {iddic = [LHAFNetWork responseConfiguration:responseObject]; success(task,dic);} failure:^(NSURLSessionDataTask* _Nullable task,NSError* _Nonnull error) { fail(task,error);}];}+(void)POST:(NSString*)url baseURL:(NSString*)baseUrl params:(NSDictionary*)paramssuccess:(LHResponseSuccess)success fail:(LHResponseFail)fail{AFHTTPSessionManager *manager = [LHAFNetWork managerWithBaseURL:baseUrl sessionConfiguration:NO];[manager POST:url parameters:params progress:^(NSProgress* _Nonnull uploadProgress) {} success:^(NSURLSessionDataTask* _Nonnull task,id_Nullable responseObject) {iddic = [LHAFNetWork responseConfiguration:responseObject]; success(task,dic);} failure:^(NSURLSessionDataTask* _Nullable task,NSError* _Nonnull error) { fail(task,error);}];}+(void)uploadWithURL:(NSString*)url params:(NSDictionary*)params fileData:(NSData*)filedata name:(NSString*)name fileName:(NSString*)filename mimeType:(NSString*) mimeType progress:(LHProgress)progress success:(LHResponseSuccess)success fail:(LHResponseFail)fail{AFHTTPSessionManager *manager = [LHAFNetWork managerWithBaseURL:nilsessionConfiguration:NO];[manager POST:url parameters:params constructingBodyWithBlock:^(id _Nonnull formData) { [formData appendPartWithFileData:filedata name:name fileName:filename mimeType:mimeType];} progress:^(NSProgress* _Nonnull uploadProgress) { progress(uploadProgress);} success:^(NSURLSessionDataTask* _Nonnull task,id_Nullable responseObject) {iddic = [LHAFNetWork responseConfiguration:responseObject]; success(task,dic);} failure:^(NSURLSessionDataTask* _Nullable task,NSError* _Nonnull error) { fail(task,error);}];}+(void)uploadWithURL:(NSString*)url baseURL:(NSString*)baseurl params:(NSDictionary*)params fileData:(NSData*)filedata name:(NSString*)name fileName:(NSString*)filename mimeType:(NSString*) mimeType progress:(LHProgress)progress success:(LHResponseSuccess)success fail:(LHResponseFail)fail{AFHTTPSessionManager *manager = [LHAFNetWork managerWithBaseURL:baseurl sessionConfiguration:YES];[manager POST:url parameters:params constructingBodyWithBlock:^(id _Nonnull formData) { [formData appendPartWithFileData:filedata name:name fileName:filename mimeType:mimeType];} progress:^(NSProgress* _Nonnull uploadProgress) { progress(uploadProgress);} success:^(NSURLSessionDataTask* _Nonnull task,id_Nullable responseObject) {iddic = [LHAFNetWork responseConfiguration:responseObject]; success(task,dic); success(task,responseObject);} failure:^(NSURLSessionDataTask* _Nullable task,NSError* _Nonnull error) { fail(task,error);}];}+(NSURLSessionDownloadTask*)downloadWithURL:(NSString*)url savePathURL:(NSURL*)fileURL progress:(LHProgress )progress success:(void(^)(NSURLResponse*,NSURL*))success fail:(void(^)(NSError*))fail{AFHTTPSessionManager *manager = [selfmanagerWithBaseURL:nilsessionConfiguration:YES];NSURL*urlpath = [NSURLURLWithString:url];NSURLRequest*request = [NSURLRequestrequestWithURL:urlpath];NSURLSessionDownloadTask*downloadtask = [manager downloadTaskWithRequest:request progress:^(NSProgress* _Nonnull downloadProgress) { progress(downloadProgress);} destination:^NSURL* _Nonnull(NSURL* _Nonnull targetPath,NSURLResponse* _Nonnull response) {return[fileURL URLByAppendingPathComponent:[response suggestedFilename]];} completionHandler:^(NSURLResponse* _Nonnull response,NSURL* _Nullable filePath,NSError* _Nullable error) {if(error) { fail(error); }else{ success(response,filePath); }}];[downloadtask resume];returndownloadtask;}#pragma mark - Private+(AFHTTPSessionManager *)managerWithBaseURL:(NSString*)baseURL sessionConfiguration:(BOOL)isconfiguration{NSURLSessionConfiguration*configuration = [NSURLSessionConfigurationdefaultSessionConfiguration];AFHTTPSessionManager *manager =nil;NSURL*url = [NSURLURLWithString:baseURL];if(isconfiguration) { manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url sessionConfiguration:configuration];}else{ manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];}manager.requestSerializer = [AFHTTPRequestSerializer serializer];manager.responseSerializer = [AFHTTPResponseSerializer serializer];returnmanager;}+(id)responseConfiguration:(id)responseObject{NSString*string = [[NSStringalloc] initWithData:responseObject encoding:NSUTF8StringEncoding];string = [string stringByReplacingOccurrencesOfString:@"\n"withString:@""];NSData*data = [string dataUsingEncoding:NSUTF8StringEncoding];NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableLeaveserror:nil];returndic;}@end