DEMO 簡介:
AFN 與 MBProgressHUD(注意版本號:version:1.0.0) 的組合封裝。
DEMO下載地址:https://github.com/IMCCP/CCPAFNNetworking
DEMO 描述:
AFN與MBProgressHUD的組合使用掸读。加載動(dòng)畫都是從度娘上下載的姜钳,效果不是很好只用作展示续室。 demo中 用到的第三方框架:AFNetworking,MBProgressHUD,MJRefresh,MJExtension,DGActivityIndicatorView
DEMO GIF:
DEMO中各個(gè)方法介紹:
1.MBProgressHUD 中添加兩個(gè)方法
/**
* ?隱藏 HUD
*/
+ (void) dissmissShowView:(UIView *)showView {
if (showView == nil) {
showView = (UIView*)[[[UIApplication sharedApplication]delegate]window];
}
[self hideHUDForView:showView animated:YES];
}
/**
* ?顯示 HUD
* ?@param imageArr ? loading 圖片數(shù)組
* ?@param showView ? HUD 展示的View
*/
+ (instancetype) showHUDWithImageArr:(NSMutableArray *)imageArr andShowView:(UIView *)showView {
if (showView == nil) {
showView ?= (UIView *)[[UIApplication sharedApplication].delegate window];
}
if (imageArr == nil) {
return [self showHUDAddedTo:showView animated:YES];
} else {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:showView animated:YES];
hud.mode = MBProgressHUDModeCustomView;
UIImageView *imaegCustomView = [[UIImageView alloc] init];
imaegCustomView.animationImages = imageArr;
[imaegCustomView setAnimationRepeatCount:0];
[imaegCustomView setAnimationDuration:(imageArr.count + 1) * 0.075];
[imaegCustomView startAnimating];
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.color = [UIColor clearColor];
hud.customView = imaegCustomView;
hud.square = NO;
return hud;
}
}
2.MBProgressHUD分類(MBProgressHUD+ADD.h)方法介紹
/**
* ?展示信息
* ?@param information 提示文字
* ?@param view ? ? ? ?HUD展示的view
* ?@param afterDelay ?展示的時(shí)間
*/
+ (MBProgressHUD *)showInformation:(NSString *)information toView:(UIView *)view andAfterDelay:(float)afterDelay {
if (view == nil) view = [UIApplication sharedApplication].keyWindow;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
;
hud.mode = MBProgressHUDModeText;
hud.label.text = information;
hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
[hud hideAnimated:YES afterDelay:afterDelay];
return hud;
}
/**
* ?展示自定義view
* ?@param customview 自定義的view
* ?@param textString 提示文字
* ?@param view ? ? ? HUD展示的view
* ?@param afterDelay 展示時(shí)間
*/
+ (void)showCustomview:(UIView *)customview andTextString:(NSString *)textString toView:(UIView *)view andAfterDelay:(float)afterDelay {
if (view == nil) view = [UIApplication sharedApplication].keyWindow;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.mode = MBProgressHUDModeCustomView;
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.color = [UIColor clearColor];
hud.customView = customview;
hud.square = YES;
hud.label.text = textString;
hud hideAnimated:YES afterDelay:afterDelay];
}
3.網(wǎng)絡(luò)請求工具類方法介紹
/**
* ?單例 返回網(wǎng)絡(luò)請求工具類對象
*/
+ (CCPNetworking *)sharedCCPNetworking;
/**
* ?開啟網(wǎng)絡(luò)監(jiān)測晰韵,監(jiān)聽網(wǎng)絡(luò)狀態(tài)的改變
*/
+ (void)startMonitoring;
/**
* ?獲取網(wǎng)絡(luò)狀態(tài)
*/
+ (NetworkStatu)checkNetStatus;
/**
* ?檢測是否有網(wǎng)絡(luò)連接
*/
+ (BOOL) isHaveNetwork;
/**
* ?post 或者 get 請求方法,block回調(diào)
* ?@param httpMethod ? ? ? 網(wǎng)絡(luò)請求類型
* ?@param url ? ? ? ? ? ? ?請求鏈接逛尚,根路徑
* ?@param params ? ? ? ? ? 參數(shù)字典
* ?@param loadingImageArr ?loading圖片數(shù)組 搪花,如果數(shù)組為 nil 則顯示默認(rèn)的loading加載動(dòng)畫
* ?@param showView ? ? ? ? HUD 展示view
* ?@param success ? ? ? ? ?請求成功返回?cái)?shù)據(jù)
* ?@param fail ? ? ? ? ? ? 請求失敗
* ?@param showHUD ? ? ? ? ?是否顯示HUD
*/
+(CCPURLSessionTask *)getOrPostWithType:(httpMethod)httpMethod ? WithUrl:(NSString *)url params:(NSDictionary *)params loadingImageArr:(NSMutableArray *)loadingImageArr ?toShowView:(UIView *)showView success:(CCPResponseSuccess)success fail:(CCPResponseFail)fail showHUD:(BOOL)showHUD;
/**
* ?上傳圖片方法 支持多張和單張上傳
* ?@param image ? ? ?上傳的圖片數(shù)組
* ?@param url ? ? ? ?請求連接,根路徑
* ?@param filename ? 圖片的名稱(如果不傳則以當(dāng)前時(shí)間命名)
* ?@param names ? ? ?上傳圖片時(shí)參數(shù)數(shù)組 <后臺 處理文件的[字段]>
* ?@param params ? ? 參數(shù)字典
* ?@param loadingImageArr ?loading圖片數(shù)組 敛熬,如果數(shù)組為 nil 則顯示默認(rèn)的loading加載動(dòng)畫
* ?@param showView ?HUD 展示view
* ?@param progress ? 上傳進(jìn)度
* ?@param success ? ?請求成功返回?cái)?shù)據(jù)
* ?@param fail ? ? ? 請求失敗返回?cái)?shù)據(jù)
* ?@param showHUD ? ?是否顯示HUD
*/
+ (CCPURLSessionTask *)uploadWithImages:(NSArray *)imageArr url:(NSString *)url filename:(NSString *)filename names:(NSArray *)nameArr params:(NSDictionary *)params loadingImageArr:(NSMutableArray *)loadingImageArr toShowView:(UIView *)showView progress:(CCPUploadProgress)progress success:(CCPResponseSuccess)success fail:(CCPResponseFail)fail showHUD:(BOOL)showHUD;
/**
* ?下載文件方法
* ?@param url ? ? ? ? ? 下載地址
* ?@param saveToPath ? ?文件保存的路徑,如果不傳則保存到Documents目錄下肺稀,以文件本來的名字命名
* ?loadingImageArr ? ? ?loading圖片數(shù)組,如果數(shù)組為 nil 則顯示默認(rèn)的loading加載動(dòng)畫
* ?@param showView ? ? ?HUD 展示view
* ?@param progressBlock 下載進(jìn)度回調(diào)
* ?@param success ? ? ? 下載完成
* ?@param fail ? ? ? ? ?失敗
* ?@param showHUD ? ? ? 是否顯示HUD
* ?@return ? ? ? ? ? ? ?返回請求任務(wù)對象应民,便于操作
*/
+ (CCPURLSessionTask *)downloadWithUrl:(NSString *)url saveToPath:(NSString *)saveToPath loadingImageArr:(NSMutableArray *)loadingImageArr progress:(CCPDownloadProgress )progressBlock toShowView:(UIView *)showView success:(CCPResponseSuccess )success failure:(CCPResponseFail )fail showHUD:(BOOL)showHUD;
注:方法的具體實(shí)現(xiàn)步驟不再在這里進(jìn)行展示话原,浪費(fèi)大家的時(shí)間,DEMO中已經(jīng)做了很詳細(xì)的標(biāo)注诲锹,大家可以下載查閱繁仁。
DEMO 使用示例
//顯示內(nèi)容為新浪新聞圖片欄目的部分?jǐn)?shù)據(jù)
在這里用到了 Charles 這款強(qiáng)大的抓包工具軟件?
Charles(version:4.0) 下載鏈接:(Charles + 破解文件,提取密碼:he3x)
Charles 使用教程:感謝原文作者的辛勤付出
- (void)loadData {
[CCPNetworking getOrPostWithType:GET WithUrl:@"http://newsapi.sina.cn/?resource=feed&accessToken=&chwm=3023_0001&city=CHXX0008&connectionType=2&deviceId=3d91d5d90c90486cde48597325cf846b699ceb53&deviceModel=apple-iphone5&from=6053093012&idfa=7CE5628E-577A-4A0E-B9E5-283217ECA1F1&idfv=10E31C9D-59AE-4547-BDEF-5FF3EA045D86&imei=3d91d5d90c90486cde48597325cf846b699ceb53&location=39.998602%2C116.365189&osVersion=9.3.5&resolution=640x1136&token=61903050f1141245bfb85231b58e84fb586743436ceb50af9f7dfe17714ee6f7&ua=apple-iphone5__SinaNews__5.3__iphone__9.3.5&weiboSuid=&weiboUid=&wm=b207&rand=221&urlSign=3c861405dd&behavior=manual&channel=news_pic&lastTimestamp=1473578882&listCount=20&p=1&pullDirection=down&pullTimes=8&replacedFlag=1&s=20" params:nil loadingImageArr:imageArr toShowView:self.view success:^(id response) {
//成功的回調(diào)归园,在這里進(jìn)行數(shù)據(jù)的解析
} fail:^(NSError *error) {
//失敗的回調(diào) 黄虱,提示用戶錯(cuò)誤信息
} showHUD:YES];
}
感謝您的閱讀,期待您的 Star庸诱,如果在使用中您有任何問題捻浦,可以在 github issues,我會(huì)盡自己能力給您答復(fù) 晤揣。