本demo傳送門 AFNetworking傳送門 MBProgressHUD傳送門
分別對AFNetworking晶渠,MBProgressHUD橙凳,進行了二次封裝,減少了代碼的冗余查辩,提高代碼可維護性轨奄,調用簡單,廢話不多說直接上代碼,具體請看demo赚哗。
Get請求
不要忘了導入#import "AFNetworking.h" "MBProgressHUD+MJ.h"
/**
GET數(shù)據(jù)請求
@param urlString URL
@param parameters 參數(shù)
@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];
// 收到數(shù)據(jù)的格式(data) 注意 :不加這句會報錯Request failed: unacceptable content-type: text/plain”錯誤嗦锐,因為我們要獲取text/plain類型數(shù)據(jù)
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數(shù)據(jù)請求
@param urlString URL
@param parameters 參數(shù)
@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 個數(shù)粤剧,順序匹配
/**
單張圖片或者多張圖片上傳
@param urlString URL
@param parameters 參數(shù)
@param imageArray 圖片數(shù)組 @[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];
}];
}
網(wǎng)絡檢測
/**
實時監(jiān)測網(wǎng)絡變化
@param netStatus 當前網(wǎng)絡狀態(tài)
*/
+ (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(@"未知網(wǎng)絡類型");
}
break;
case AFNetworkReachabilityStatusNotReachable:
if (netStatus) {
netStatus(@"無可用網(wǎng)絡");
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
if (netStatus) {
netStatus(@"當前WIFE下");
}
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
if (netStatus) {
netStatus(@"使用蜂窩流量");
}
break;
default:
break;
}
}];
[manager startMonitoring];
}
簡單調用
- 網(wǎng)絡請求
/**
網(wǎng)絡請求(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:@"純展示"];
}