xcode iOS 应用程序 - 设置 UIWebView 加载超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11615699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS App - Set Timeout for UIWebView loading
提问by adamdehaven
I have a simple iOS native app that loads a single UIWebView. I would like the webView to show an error message if the app doesn't COMPLETELY finish loading the initial page in the webView within 20 seconds.
我有一个加载单个 UIWebView 的简单 iOS 本机应用程序。如果应用程序未在 20 秒内完全加载 webView 中的初始页面,我希望 webView 显示错误消息。
I load my URL for the webView within my viewDidLoad
like this (simplified):
我viewDidLoad
像这样加载 webView 的 URL (简化):
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
The timeoutInterval
within the code above does not actually "do" anything, as Apple has it set within the OS to not actually time out for 240 seconds.
该timeoutInterval
代码在上述范围内实际上并没有“做”什么,因为苹果有它在操作系统中设置为不实际超时240秒。
I have my webView didFailLoadWithError
actions set, but if the user HAS a network connection, this never gets called. The webView just continues to try loading with my networkActivityIndicator spinning.
我设置了我的webView didFailLoadWithError
操作,但是如果用户有网络连接,则永远不会调用它。webView 只是继续尝试加载我的 networkActivityIndicator 旋转。
Is there a way to set a timeout for the webView?
有没有办法为 webView 设置超时?
回答by mask8
The timeoutInterval is for connection. Once webview connected to the URL, you'll need to start NSTimer and do your own timeout handling. Something like:
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];
}
回答by Sandy Chapman
All of the suggested solutions are not ideal. The correct way to handle this is using the timeoutInterval on the NSMutableURLRequest
itself:
所有建议的解决方案都不理想。处理这个问题的正确方法是在自身上使用 timeoutInterval NSMutableURLRequest
:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://web.site"]];
request.timeoutInterval = 10;
[webview loadRequest:request];
回答by LE SANG
My way is similar to accepted answer but just stopLoading when time out and control in didFailLoadWithError.
我的方式类似于接受的答案,但只是在超时时停止加载并在 didFailLoadWithError 中进行控制。
- (void)timeout{
if ([self.webView isLoading]) {
[self.webView stopLoading];//fire in didFailLoadWithError
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
self.timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(timeout) userInfo:nil repeats:NO];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self.timer invalidate];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
//Error 999 fire when stopLoading
[self.timer invalidate];//invalidate for other errors, not time out.
}
回答by Codetard
Swift coders can do it as follow:
Swift 编码人员可以按照以下方式进行:
var timeOut: NSTimer!
func webViewDidStartLoad(webView: UIWebView) {
self.timeOut = NSTimer.scheduledTimerWithTimeInterval(7.0, target: self, selector: "cancelWeb", userInfo: nil, repeats: false)
}
func webViewDidFinishLoad(webView: UIWebView) {
self.timeOut.invalidate()
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
self.timeOut.invalidate()
}
func cancelWeb() {
print("cancelWeb")
}