Xcode iOS应用程序-设置UIWebView加载超时
我有一个简单的iOS本机应用程序,可以加载单个UIWebView。我希望webView显示一条错误消息,如果应用程序没有在20秒内完全完成在webView中加载初始页面 我在我的Xcode iOS应用程序-设置UIWebView加载超时,xcode,ios5,uiwebview,timeout,loading,Xcode,Ios5,Uiwebview,Timeout,Loading,我有一个简单的iOS本机应用程序,可以加载单个UIWebView。我希望webView显示一条错误消息,如果应用程序没有在20秒内完全完成在webView中加载初始页面 我在我的viewdiload中加载Web视图的URL,如下所示(简化): 上面代码中的timeoutInterval实际上并没有“做”任何事情,因为苹果在操作系统中设置了240秒不超时 我设置了我的webView didFailLoadWithError操作,但如果用户有网络连接,则不会调用该操作。webView只是继续尝试加载
viewdiload
中加载Web视图的URL,如下所示(简化):
上面代码中的timeoutInterval
实际上并没有“做”任何事情,因为苹果在操作系统中设置了240秒不超时
我设置了我的webView didFailLoadWithError
操作,但如果用户有网络连接,则不会调用该操作。webView只是继续尝试加载我的networkActivityIndicator
有没有办法为webView设置超时 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];
}
我的方法类似于公认的答案,但只是在超时时停止加载,并在DIDFILLOADWITHERROR中进行控制
- (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.
}
Swift编码人员可以按如下方式进行:
var timeOut: NSTimer!
func webViewDidStartLoad(webView: UIWebView) {
self.timeOut = Timer.scheduledTimer(timeInterval: 7.0, target: self, selector: 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")
}
所有建议的解决方案都不理想。正确的处理方法是在
NSMutableURLRequest
本身上使用timeoutInterval:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://web.site"]];
request.timeoutInterval = 10;
[webview loadRequest:request];
这里有人说超时时间是用来连接的。一旦它连接到服务器,就不会再抛出错误。您需要在webViewDidStartLoad中连接并自行取消后实现自己的NSTimer,这看起来正是我要查找的。。。你能给我一些如何实施的指导吗?(我是Objective-C新手)我只需要知道该怎么做:
-(void)webView:NSTimer*timer{NSLog(@“我在这里延迟1分钟”);}
我会使用哪种目标方法?我刚刚编写了一个非常基本的代码,你可能想从它开始:)希望它能帮助我添加上述方法,但20秒后它什么也没做我在cancelWeb
属性中放入了一个UIAlertView,但它从未启动。很抱歉,我做了一个更改,不是NSTimer timerWithTimeInterval
,而是scheduledTimerWithTimeInterval
。一旦webView连接到URL,就会调用webViewDidStartLoad
。如果不是,则说明您没有正确设置委托。然后该方法启动计时器,在20秒内调用cancelWeb
方法。非常感谢!!!工作完美:)还有一个问题-如果他们在视图中退出应用程序,我是否需要做任何事情来释放计时器将消失
或viewDidUnload
?您可以使用某种标志变量跳过第一个webViewDidStartLoad。当用户单击第一页中的链接时,webView将启动并每次再次调用webViewDidStartLoad。您需要通过用户的单击来确定它是第一次加载还是以后加载。或者您可以插入js来处理用户的点击,但这可能太多了。我希望这能回答你的问题哦,我明白了。那太糟糕了。您可能需要在jquerymobile上使用b/w代码和js代码进行通信。我会使用stringByEvaluatingJavaScriptFromString:,或者这可能会起作用。我甚至还考虑了PrimeGAP或者某种WebVIEW基础框架。很抱歉,我帮不上忙,这是更好的答案,因为它实际上会在超时后停止web视图。我还建议在使其无效并完成后,将self.timer
设置为nil
。此调用是否会在超时后调用didFailToLoad withError
?@alberrenshaw是的,它会自动调用委托方法
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://web.site"]];
request.timeoutInterval = 10;
[webview loadRequest:request];