本次封装只是简单的对网络请求封装,未涉及数据缓存的功能,其代码实现如下所示:
在XYNetworking.h文件中:
#import <Foundation/Foundation.h>
#import "AFHTTPSessionManager.h"
/**
* 请求成功所走方法
*
* @param response 请求返还的数据
*/
typedef void (^XYResponseSuccess)(NSURLSessionDataTask * task,id responseObject);
/**
* 请求错误所走方法
*
* @param error 请求错误返还的信息
*/
typedef void (^XYResponseFail)(NSURLSessionDataTask * task, NSError * error);
typedef void (^XYProgress)(NSProgress *progress);
@interface XYNetworking : NSObject
/**
* 普通get方法请求网络数据
*
* @param url 请求网址路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/
+(void)GET:(NSString *)url
params:(NSDictionary *)params
success:(XYResponseSuccess)success
fail:(XYResponseFail)fail;
/**
* 含有baseURL的get方法
*
* @param url 请求网址路径
* @param baseUrl 请求网址根路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/
+(void)GET:(NSString *)url
baseURL:(NSString *)baseUrl
params:(NSDictionary *)params
success:(XYResponseSuccess)success
fail:(XYResponseFail)fail;
/**
* 普通post方法请求网络数据
*
* @param url 请求网址路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/
+(void)POST:(NSString *)url
params:(NSDictionary *)params
success:(XYResponseSuccess)success
fail:(XYResponseFail)fail;
/**
* 含有baseURL的post方法
*
* @param url 请求网址路径
* @param baseUrl 请求网址根路径
* @param params 请求参数
* @param success 成功回调
* @param fail 失败回调
*/
+(void)POST:(NSString *)url
baseURL:(NSString *)baseUrl
params:(NSDictionary *)params
success:(XYResponseSuccess)success
fail:(XYResponseFail)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:(XYProgress)progress
success:(XYResponseSuccess)success
fail:(XYResponseFail)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:(XYProgress)progress
success:(XYResponseSuccess)success
fail:(XYResponseFail)fail;
/**
* 下载文件
*
* @param url 请求网络路径
* @param fileURL 保存文件url
* @param progress 下载进度
* @param success 成功回调
* @param fail 失败回调
*
* @return 返回NSURLSessionDownloadTask实例,可用于暂停继续,暂停调用suspend方法,重新开启下载调用resume方法
*/
+(NSURLSessionDownloadTask *)downloadWithURL:(NSString *)url
savePathURL:(NSURL *)fileURL
progress:(XYProgress )progress
success:(void (^)(NSURLResponse *, NSURL *))success
fail:(void (^)(NSError *))fail;
@end
在XYNetworking.m文件中:
#import "XYNetworking.h"
@interface XYNetworking()
@end
@implementation XYNetworking
#pragma mark - Public
+(void)GET:(NSString *)url params:(NSDictionary *)params
success:(XYResponseSuccess)success fail:(XYResponseFail)fail{
AFHTTPSessionManager *manager = [XYNetworking managerWithBaseURL:nil sessionConfiguration:NO];
[manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id dic = [XYNetworking 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:(XYResponseSuccess)success fail:(XYResponseFail)fail{
AFHTTPSessionManager *manager = [XYNetworking managerWithBaseURL:baseUrl sessionConfiguration:NO];
[manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id dic = [XYNetworking responseConfiguration:responseObject];
success(task,dic);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
fail(task,error);
}];
}
+(void)POST:(NSString *)url params:(NSDictionary *)params
success:(XYResponseSuccess)success fail:(XYResponseFail)fail{
AFHTTPSessionManager *manager = [XYNetworking managerWithBaseURL:nil sessionConfiguration:NO];
[manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id dic = [XYNetworking 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:(XYResponseSuccess)success fail:(XYResponseFail)fail{
AFHTTPSessionManager *manager = [XYNetworking managerWithBaseURL:baseUrl sessionConfiguration:NO];
[manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id dic = [XYNetworking 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:(XYProgress)progress
success:(XYResponseSuccess)success
fail:(XYResponseFail)fail{
AFHTTPSessionManager *manager = [XYNetworking 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 = [XYNetworking 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:(XYProgress)progress
success:(XYResponseSuccess)success
fail:(XYResponseFail)fail{
AFHTTPSessionManager *manager = [XYNetworking 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 = [XYNetworking responseConfiguration:responseObject];
success(task,dic);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
fail(task,error);
}];
}
+(NSURLSessionDownloadTask *)downloadWithURL:(NSString *)url
savePathURL:(NSURL *)fileURL
progress:(XYProgress )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
封装包括基本的网络数据请求,文件上传,文件下载,其中含有🌰
demo地址
PS:如需转载请注明iOS小乔 http://www.jianshu.com/p/986f51578e96
如果能够帮到你,请你打赏支持我的创作(请小乔喝杯饮料😁)