webview加载 怎么设置超时 ios

 我来答
蘑菇饭资讯
推荐于2016-05-15 · TA获得超过6万个赞
知道大有可为答主
回答量:1.7万
采纳率:90%
帮助的人:1.2亿
展开全部
时间你自己改,代码里面我用的是5秒

<pre class="brush:objc; toolbar: true; auto-links: false;">

#pragma mark - nsurl connection method and delegate

-(void)checkConnection
{
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:tvUrl]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:5.0];
if (theConnection)
{
[theConnection cancel];
SAFE_RELEASE(theConnection);
}
theConnection= [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if (theConnection) {
SAFE_RELEASE(theConnection);
}

if ([response isKindOfClass:[NSHTTPURLResponse class]]){

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ((([httpResponse statusCode]/100) == 2)){
DLog(@"connection ok");
}
else{
NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:nil];
if ([error code] == 404){
NSLog(@"404");
[self openNextLink];
}
}
}
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

if (theConnection) {
SAFE_RELEASE(theConnection);
}
if (loadNotFinishCode == NSURLErrorCancelled) {
return;
}
if (error.code == 22) //The operation couldn’t be completed. Invalid argument
[self openNextLink];
else if (error.code == -1001) //The request timed out. webview code -999的时候会收到-1001
[self openNextLink];
else if (error.code == -1005) //The network connection was lost.
[self openNextLink];
else if (error.code == -1009){ //The Internet connection appears to be offline
//do nothing
}
}

</pre>
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式