iOS中設(shè)置網(wǎng)絡(luò)超時時間+模擬的方法詳解
設(shè)置方法如下:
在封裝的網(wǎng)絡(luò)請求類里面如下設(shè)置
AFWEBAPI_REQUEST_TIMEOUT 這個參數(shù)為超時時間
#define AFWEBAPI_REQUEST_TIMEOUT 20 #pragma mark - 單例 & 構(gòu)造函數(shù) + (instancetype)sharedTools { static WXNetworkTools *instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] initWithBaseURL:[NSURL URLWithString:WX_SERVER_URL]]; instance.requestSerializer.timeoutInterval = AFWEBAPI_REQUEST_TIMEOUT; }); return instance; }
在封裝的方法里面,在失敗的回調(diào)里面寫下如下代碼,至于怎么處置就看自己了,我這里具體需求沒有給,我先做了一個彈框處理
/// @param finished 完成回調(diào) - (void)requestWithMethod:(WXRequestMethod)method URLString:(NSString *)URLString parameters:(id)parameters finished:(WXRequestCallBack)finished { NSString *methodName = (method == GET) ? @"GET" : @"POST"; NSLog(@"%@",URLString); [[self dataTaskWithHTTPMethod:methodName URLString:URLString parameters:parameters uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, id responseObject) { finished(responseObject, nil); if (responseObject[@"status"] && [responseObject[@"status"] integerValue] == 1000) { //這里來賬號互踢 [[NSNotificationCenter defaultCenter] postNotificationName:KMutualKickNotification object:nil]; return; } } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"網(wǎng)絡(luò)請求錯誤 %@", error); //這里來處理網(wǎng)絡(luò)超時 if (error.code == -1001) { [SVProgressHUD showErrorWithStatus:@"網(wǎng)絡(luò)超時!"]; return ; } finished(nil, error); }] resume]; }
如何去調(diào)試呢,難道去電梯里面去調(diào)試嗎?
下面截圖教你們?nèi)绾稳プ鼍W(wǎng)絡(luò)限制去摸你用戶網(wǎng)絡(luò)不好的情況
使用工具:charles(青花瓷)
頂端的工具條--》Proxy --》Throttling Settting
設(shè)置好之后千萬要記得去勾選Throttling,不然沒有效果
頂端的工具條--》Proxy --》Throttling
這樣就ok了
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位iOS開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS overFullScreen與fullScreen區(qū)別分析
這篇文章主要介紹了iOS overFullScreen與fullScreen區(qū)別分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11IOS中UIWebView加載Loading的實現(xiàn)方法
最近有朋友問我類似微信語音播放的喇叭動畫和界面圖片加載loading界面是怎樣實現(xiàn)的,是不是就是一個gif圖片呢!我的回答當(dāng)然是否定了,當(dāng)然不排除也有人用gif圖片啊!2015-05-05