ada*_*ven 23 xcode timeout loading uiwebview ios5
我有一个简单的iOS本机应用程序,可以加载单个UIWebView.如果应用程序没有完全在20秒内完成在webView中加载初始页面,我希望webView显示错误消息.
我在这里加载了我的webView viewDidLoad(简化):
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
Run Code Online (Sandbox Code Playgroud)
该timeoutInterval代码在上述范围内实际上并没有"做"什么,因为苹果有它在操作系统中设置为不实际超时240秒.
我设置了自己的webView didFailLoadWithError操作,但如果用户有网络连接,则永远不会调用它.webView继续尝试使用我的networkActivityIndicator旋转加载.
有没有办法为webView设置超时?
mas*_*sk8 41
timeoutInterval用于连接.一旦webview连接到URL,您将需要启动NSTimer并执行自己的超时处理.就像是:
// define NSTimer *timer; somewhere in your class
- (void)cancelWeb
{
NSLog(@"didn't finish loading within 20 sec");
// do anything error
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[timer invalidate];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
// webView connected
timer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(cancelWeb) userInfo:nil repeats:NO];
}
Run Code Online (Sandbox Code Playgroud)
所有建议的解决方案都不理想.处理此问题的正确方法是在自身上使用timeoutInterval NSMutableURLRequest:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://web.site"]];
request.timeoutInterval = 10;
[webview loadRequest:request];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29882 次 |
| 最近记录: |