iOS开发AFNetworking 与 MBProgressHUD 的二次封装

本demo传送门 AFNetworking传送门 MBProgressHUD传送门

分别对AFNetworking,MBProgressHUD,进行了二次封装,减少了代码的冗余,提高代码可维护性,调用简单,废话不多说直接上代码,具体请看demo。

Get请求

不要忘了导入#import “AFNetworking.h” “MBProgressHUD+MJ.h”

/**
GET数据请求
@param urlString  URL
@param parameters 参数
@param success    成功回调
@param failure    失败回调
*/
+ (void)GET:(NSString *)urlString parameters:(id)parameters success:(void (^) (id responseObject))success failure:(void (^) (NSError *error))failure;
+ (void)GET:(NSString *)urlString parameters:(id)parameters success:(void (^) (id responseObject))success failure:(void (^) (NSError *error))failure
{
[MBProgressHUD showMessage:@"拼命加载中..."];
// 初始化管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    上传普通的格式
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//    收到数据的格式(data)  注意 :不加这句会报错Request failed: unacceptable content-type: text/plain”错误,因为我们要获取text/plain类型数据
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", nil];
manager.requestSerializer.timeoutInterval = 10.0f;
urlString  = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[manager GET:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
if (success) {
success(responseObject);
}
[MBProgressHUD hideHUD];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
[MBProgressHUD hideHUD];
}];
}

POST请求

/**
POST数据请求
@param urlString  URL
@param parameters 参数
@param success    成功回调
@param failure    失败回调
*/
+ (void)POST:(NSString *)urlString parameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^) (NSError *error))failure;
+ (void)POST:(NSString *)urlString parameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^) (NSError *error))failure
{
[MBProgressHUD showMessage:@"拼命加载中..."];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", nil];
manager.requestSerializer.timeoutInterval = 10.0f;
urlString  = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[manager POST:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
if (success ) {
success(dic);
}
[MBProgressHUD hideHUD];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
[MBProgressHUD hideHUD];
}];
}

UPLoad (图片上传)

多个字段 对应 多张图片上传时,要保持 imageArray,imageKeys 个数,顺序匹配

/**
单张图片或者多张图片上传
@param urlString URL
@param parameters 参数
@param imageArray 图片数组 @[UIImage]
@param imageKeys 上传图片对应的 key
@param successs 成功回调
@param failure 失败回调
*/
+ (void)uploadMorePost:(NSString *)urlString parameters:(id)parameters UploadImage:(NSArray *)imageArray ImageKey:(NSArray *)imageKeys success:(void (^)(id responseObject))successs failure:(void (^)(NSError *error))failure;
+ (void)uploadMorePost:(NSString *)urlString parameters:(id)parameters UploadImage:(NSArray *)imageArray ImageKey:(NSArray *)imageKeys success:(void (^)(id responseObject))successs failure:(void (^)(NSError *error))failure
{
if (imageKeys.count == 0) {
[MBProgressHUD showError:[NSString stringWithFormat:@"%@为空",imageKeys]];
return ;
}
if (imageArray.count == 0) {
[MBProgressHUD showError:[NSString stringWithFormat:@"%@为空",imageArray]];
return;
}
[MBProgressHUD showMessage:@"拼命加载中..."];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer.timeoutInterval = 10.f;
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", nil];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
urlString  = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[manager POST:urlString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (int i = 0; i < imageArray.count; i++) {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
UIImage *image = imageArray[i];
NSData *data = UIImageJPEGRepresentation(image, 0.7);
[formData appendPartWithFileData:data name:((imageKeys.count > 1) ? (imageKeys[i]) : (imageKeys.firstObject)) fileName:fileName mimeType:@"image/png"];
}
} success:^(NSURLSessionDataTask *task, id responseObject) {
if (successs) {
successs(responseObject);
}
[MBProgressHUD hideHUD];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
[MBProgressHUD hideHUD];
}];
}

网络检测

/**
实时监测网络变化
@param netStatus 当前网络状态
*/
+ (void)ReachabilityStatus:(void (^)(id string))netStatus;
+ (void)ReachabilityStatus:(void (^)(id string))netStatus
{
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
if (netStatus) {
netStatus(@"未知网络类型");
}
break;
case AFNetworkReachabilityStatusNotReachable:
if (netStatus) {
netStatus(@"无可用网络");
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
if (netStatus) {
netStatus(@"当前WIFE下");
}
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
if (netStatus) {
netStatus(@"使用蜂窝流量");
}
break;
default:
break;
}
}];
[manager startMonitoring];
}

简单调用

  • 网络请求
/**
网络请求(Get,Post,upload(上传图片)); (IBAction)RequestData:(UIButton *)sender(点击事件)
*/
- (IBAction)RequestData:(UIButton *)sender {
[BasicNetWorking GET:@"http://123.56.182.186/oow/api/home/data.do?uid=31" parameters:nil success:^(id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSError *error) {
NSLog(@"%@",error.localizedDescription);
}];
}
  • MBProgressHUD成功提示
- (IBAction)RequestSuccess:(UIButton *)sender {
[MBProgressHUD showSuccess:@"成功了"];
}
  • MBProgressHUD失败提示
- (IBAction)RequestFail:(UIButton *)sender {
[MBProgressHUD showError:@"失败了"];
}
  • MBProgressHUD纯文本提示
- (IBAction)AlertMessage:(UIButton *)sender {
[MBProgressHUD showAlert:@"纯展示"];
}

 

Simulator Screen Shot 2017年9月6日 下午6.27.27.png
Simulator Screen Shot 2017年9月6日 下午6.28.02.png
Simulator Screen Shot 2017年9月6日 下午6.28.05.png
Simulator Screen Shot 2017年9月6日 下午6.28.08.png
特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!
技术狗 » iOS开发AFNetworking 与 MBProgressHUD 的二次封装