webview加载 怎么设置超时 ios
1个回答
展开全部
时间你自己改,代码里面我用的是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>
<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>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询