摘要
随着苹果https的出现,在网络请求方面苹果已经抛弃了NSURLConnection,而是采用了使用更为简单的NSURLSession,最近正在搭一个程序框架,最后还是选择了AFN3.0,并通过朋友demo的参考,进行了二次封装,集成方法如下:
准备阶段
1.首先下载最新版本的AFN3.0,网址如下:https://github.com/AFNetworking/AFNetworking
2.新建一个工程:名字随意
集成阶段
1.打开下载下来的demo,运行如下(其中红色箭头所指的文件是我们拖到项目中的两个文件):
2.将拖到新建的项目中,在info.plist文件中添加ATS(App Transporty security……),command+B编译一下,发现没有问题即可。ATS截图如下:
到目前来说,AFN3.0已经集成完毕,但是有些细心的朋友会发现,AFN3.0的工程中出现了好多SSL证书,如图所示:
这种情况下:自己也爬网查了一些东西,提供两个网址以供参考:
http://www.cnblogs.com/jys509/p/5001566.html
http://oncenote.com/2014/10/21/Security-1-HTTPS/
讲的很专业,希望对读到的朋友有帮助!
二次封装
主要是新建了一个NSObject的子类,对AFN的功能进行了集成:
LHAFNetWork.h
#import <Foundation/Foundation.h>
#import "AFNetworking/AFNetworking.h"
/**
* 宏定义请求成功的block
*
* @param response 请求成功返回的数据
*/
typedef void (^LHResponseSuccess)(NSURLSessionDataTask * task,id responseObject);
/**
* 宏定义请求失败的block
*
* @param error 报错信息
*/
typedef void (^LHResponseFail)(NSURLSessionDataTask * task, NSError * error);
/**
* 上传或者下载的进度
*
* @param progress 进度
*/
typedef void (^LHProgress)(NSProgress *progress);
@interface LHAFNetWork : NSObject
/**
* 普通get方法请求网络数据
*
* @param url 请求网址路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/
+(void)GET:(NSString *)url
params:(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 *)baseUrl
params:(NSDictionary *)params success:(LHResponseSuccess)success fail:(LHResponseFail)fail;
/**
* 普通post方法请求网络数据
*
* @param url 请求网址路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/
+(void)POST:(NSString *)url
params:(NSDictionary *)params
success:(LHResponseSuccess)success
fail:(LHResponseFail)fail;
/**
* 含有baseURL的post方法
*
* @param url 请求网址路径
* @param baseUrl 请求网址根路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/
+(void)POST:(NSString *)url
baseURL:(NSString *)baseUrl
params:(NSDictionary *)params
success:(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"
@implementation LHAFNetWork
+(void)GET:(NSString *)url params:(NSDictionary *)params
success:(LHResponseSuccess)success fail:(LHResponseFail)fail{
AFHTTPSessionManager *manager = [LHAFNetWork managerWithBaseURL:nil sessionConfiguration:NO];
[manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id dic = [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) {
id dic = [LHAFNetWork responseConfiguration:responseObject];
success(task,dic);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
fail(task,error);
}];
}
+(void)POST:(NSString *)url params:(NSDictionary *)params
success:(LHResponseSuccess)success fail:(LHResponseFail)fail{
AFHTTPSessionManager *manager = [LHAFNetWork managerWithBaseURL:nil sessionConfiguration:NO];
[manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id dic = [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 *)params
success:(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) {
id dic = [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:nil sessionConfiguration:NO];
[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:filedata name:name fileName:filename mimeType:mimeType];
} progress:^(NSProgress * _Nonnull uploadProgress) {
progress(uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id dic = [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<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:filedata name:name fileName:filename mimeType:mimeType];
} progress:^(NSProgress * _Nonnull uploadProgress) {
progress(uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id dic = [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 = [self managerWithBaseURL:nil sessionConfiguration:YES];
NSURL *urlpath = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL: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];
return downloadtask;
}
#pragma mark - Private
+(AFHTTPSessionManager *)managerWithBaseURL:(NSString *)baseURL sessionConfiguration:(BOOL)isconfiguration{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFHTTPSessionManager *manager =nil;
NSURL *url = [NSURL URLWithString:baseURL];
if (isconfiguration) {
manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url sessionConfiguration:configuration];
}else{
manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
}
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
return manager;
}
+(id)responseConfiguration:(id)responseObject{
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
return dic;
}
@end
到此为止本人的第一篇博客已经写完,后续还会有更多高含量的博客发表……
转载:http://www.jianshu.com/p/986f51578e96
欢迎关注我的个人微信公众号,免费送计算机各种最新视频资源!你想象不到的精彩!