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應(yīng)用開發(fā)中使用NSLocale類實現(xiàn)對象信息的本地化
這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用NSLocale類實現(xiàn)對象信息的本地化的方法,能夠?qū)r間和貨幣等格式化為與系統(tǒng)本地設(shè)置相同的偏好,需要的朋友可以參考下2016-05-05

