摘要
隨著蘋果https的出現(xiàn),在網(wǎng)絡(luò)請(qǐng)求方面蘋果已經(jīng)拋棄了NSURLConnection鸳劳,而是采用了使用更為簡(jiǎn)單的NSURLSession,最近正在搭一個(gè)程序框架狰贯,最后還是選擇了AFN3.0,并通過朋友demo的參考,進(jìn)行了二次封裝涵紊,集成方法如下:
準(zhǔn)備階段
1.首先下載最新版本的AFN3.0傍妒,網(wǎng)址如下:https://github.com/AFNetworking/AFNetworking
2.新建一個(gè)工程:名字隨意
集成階段
1.打開下載下來的demo,運(yùn)行如下(其中紅色箭頭所指的文件是我們拖到項(xiàng)目中的兩個(gè)文件):
2.將拖到新建的項(xiàng)目中,在info.plist文件中添加ATS(App Transporty security……)摸柄,command+B編譯一下颤练,發(fā)現(xiàn)沒有問題即可。ATS截圖如下:
到目前來說驱负,AFN3.0已經(jīng)集成完畢嗦玖,但是有些細(xì)心的朋友會(huì)發(fā)現(xiàn),AFN3.0的工程中出現(xiàn)了好多SSL證書跃脊,如圖所示:
這種情況下:自己也爬網(wǎng)查了一些東西宇挫,提供兩個(gè)網(wǎng)址以供參考:
http://www.cnblogs.com/jys509/p/5001566.html
http://oncenote.com/2014/10/21/Security-1-HTTPS/
講的很專業(yè),希望對(duì)讀到的朋友有幫助酪术!
二次封裝
主要是新建了一個(gè)NSObject的子類器瘪,對(duì)AFN的功能進(jìn)行了集成:
LHAFNetWork.h
#import <Foundation/Foundation.h>
#import "AFNetworking/AFNetworking.h"
/**
* 宏定義請(qǐng)求成功的block
*
* @param response 請(qǐng)求成功返回的數(shù)據(jù)
*/
typedef void (^LHResponseSuccess)(NSURLSessionDataTask * task,id responseObject);
/**
* 宏定義請(qǐng)求失敗的block
*
* @param error 報(bào)錯(cuò)信息
*/
typedef void (^LHResponseFail)(NSURLSessionDataTask * task, NSError * error);
/**
* 上傳或者下載的進(jìn)度
*
* @param progress 進(jìn)度
*/
typedef void (^LHProgress)(NSProgress *progress);
@interface LHAFNetWork : NSObject
/**
* 普通get方法請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù)
*
* @param url 請(qǐng)求網(wǎng)址路徑
* @param params 請(qǐng)求參數(shù)
* @param success 成功回調(diào)
* @param fail 失敗回調(diào)
*/
+(void)GET:(NSString *)url
params:(NSDictionary *)params success:(LHResponseSuccess)success
fail:(LHResponseFail)fail;
/**
* 含有baseURL的get方法
*
* @param url 請(qǐng)求網(wǎng)址路徑
* @param baseUrl 請(qǐng)求網(wǎng)址根路徑
* @param params 請(qǐng)求參數(shù)
* @param success 成功回調(diào)
* @param fail 失敗回調(diào)
*/
+(void)GET:(NSString *)url baseURL:(NSString *)baseUrl
params:(NSDictionary *)params success:(LHResponseSuccess)success fail:(LHResponseFail)fail;
/**
* 普通post方法請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù)
*
* @param url 請(qǐng)求網(wǎng)址路徑
* @param params 請(qǐng)求參數(shù)
* @param success 成功回調(diào)
* @param fail 失敗回調(diào)
*/
+(void)POST:(NSString *)url
params:(NSDictionary *)params
success:(LHResponseSuccess)success
fail:(LHResponseFail)fail;
/**
* 含有baseURL的post方法
*
* @param url 請(qǐng)求網(wǎng)址路徑
* @param baseUrl 請(qǐng)求網(wǎng)址根路徑
* @param params 請(qǐng)求參數(shù)
* @param success 成功回調(diào)
* @param fail 失敗回調(diào)
*/
+(void)POST:(NSString *)url
baseURL:(NSString *)baseUrl
params:(NSDictionary *)params
success:(LHResponseSuccess)success
fail:(LHResponseFail)fail;
/**
* 普通路徑上傳文件
*
* @param url 請(qǐng)求網(wǎng)址路徑
* @param params 請(qǐng)求參數(shù)
* @param filedata 文件
* @param name 指定參數(shù)名
* @param filename 文件名(要有后綴名)
* @param mimeType 文件類型
* @param progress 上傳進(jìn)度
* @param success 成功回調(diào)
* @param fail 失敗回調(diào)
*/
+(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 請(qǐng)求網(wǎng)址路徑
* @param baseurl 請(qǐng)求網(wǎng)址根路徑
* @param params 請(qǐng)求參數(shù)
* @param filedata 文件
* @param name 指定參數(shù)名
* @param filename 文件名(要有后綴名)
* @param mimeType 文件類型
* @param progress 上傳進(jìn)度
* @param success 成功回調(diào)
* @param fail 失敗回調(diào)
*/
+(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 請(qǐng)求網(wǎng)絡(luò)路徑
* @param fileURL 保存文件url
* @param progress 下載進(jìn)度
* @param success 成功回調(diào)
* @param fail 失敗回調(diào)
*
* @return 返回NSURLSessionDownloadTask實(shí)例,可用于暫停繼續(xù)拼缝,暫停調(diào)用suspend方法娱局,重新開啟下載調(diào)用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
到此為止本人的第一篇博客已經(jīng)寫完,后續(xù)還會(huì)有更多高含量的博客發(fā)表……
轉(zhuǎn)載:http://www.reibang.com/p/986f51578e96
歡迎關(guān)注我的個(gè)人微信公眾號(hào)咧七,免費(fèi)送計(jì)算機(jī)各種最新視頻資源衰齐!你想象不到的精彩!