如果数据开始加载但不是 webViewDidFinishLoad,则不会触发 NSMutableURLRequest 超时 [英] NSMutableURLRequest timeout doesn't trigger if data starts to load but not webViewDidFinishLoad

查看:21
本文介绍了如果数据开始加载但不是 webViewDidFinishLoad,则不会触发 NSMutableURLRequest 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载一个 UIWebview 并请求一个可能需要一些时间才能完成加载的 URL.请求开始并开始接收数据.但是,请求似乎没有完成.webViewDidFinishLoad 永远不会触发,webView didFailLoadWithError: 回调也不会触发.

I am loading a UIWebview and requesting a URL that can take some time to finish loading. The request starts and begins to receive data. However, it doesn't appear that the request finishes. The webViewDidFinishLoad never triggers nor does the webView didFailLoadWithError: callback.

NSURLRequest 的超时是否仅在没有响应接收数据的情况下发生,或者如果请求在该时间间隔内也没有完成,它是否也会触发?

Does a timeout of a NSURLRequest only occur if there is no response receiving data or does it trigger if the request doesn't complete in that interval as well?

如果是前者,是否有一个优雅的解决方案来超时请求?

If it's the former, is there an elegant solution to timing out the request?

相关代码为:

加载请求:

<代码>-(无效)加载请求{NSString *targetURL = @"http://myrequestUrl/";NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: targetURL] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 15.0] autorelease];

- (void) loadRequest { NSString *targetURL = @"http://myrequestUrl/"; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: targetURL] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 15.0] autorelease];

[request setHTTPBasicID: [[self credentialManager] userID] password:[[self credentialManager] password]];
[webView loadRequest:request];

委托响应超时

<代码>- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {[[UIApplication sharedApplication] setNetworkActivityIndi​​catorVisible: NO];[警报隐藏];[警报自动释放];警报 = 无;NSLog(@"错误 - %@", 错误);[self showRetryPrompt: error];
}

推荐答案

NSURLRequest 的超时是为了连接.如果您已连接并且服务器只需要很长时间才能响应,那么超时对您没有好处.您需要为您想要的超时设置自己的 NSTimer 并自行取消请求.

The timeout for the NSURLRequest is for the connection. If you get connected and it just takes a long time for the server to response, he timeout will do you no good. You will need to setup your own NSTimer for the timeout you want and cancel the request yourself.

这篇关于如果数据开始加载但不是 webViewDidFinishLoad,则不会触发 NSMutableURLRequest 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆