假设有一个包含到B.html的链接的网页A.html。
如果单击了B.html,则shouldStartLoadWithRequest:中的request.URL将为B.html。但是,如果在加载页面时出现问题(例如,假设页面不存在),那么在didFailLoadWithError中: webView.request.URL的值不是B.html,而是A.html。
因此,除非我缓存最后一次页面加载,否则似乎不可能知道哪个页面加载失败了,但我原本希望webView.request.URL为B.html,因此这是一个缺陷吗?我没有看到关于它应该是什么的文档。
iOS 6
发布于 2013-10-23 03:37:43
我也有同样的问题。如果其他人也这样做,那么error.userInfo字典就可以了。
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
if (error.domain == NSURLErrorDomain) {
if (error.code == NSURLErrorCancelled) { //ignore this one, interrupted load
return;
}
}
}
//NSString *theURLString = [webView.request.URL absoluteString]; this won't work - it just returns the last successful url
NSString *theURLString = [error.userInfo objectForKey:@"NSErrorFailingURLStringKey"]; //this works文档中说NSErrorFailingURLStringKey在iOS4中已被弃用(仅用于向后兼容),您应该使用NSURLErrorFailingURLStringErrorKey。
但是,不会返回NSURLErrorFailingURLStringErrorKey (至少在我的UIWebView版本中不会)。相反,NSErrorFailingURLKey是返回URL的另一个键,但我在文档中找不到它。
发布于 2017-05-18 13:13:44
我在Swift 3.1中也遇到了同样的问题。要获取失败的url,请使用此委托方法:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool{
let failedUrl = (request.url?.absoluteString)! as String
return true
}发布于 2012-11-09 08:20:15
您说得对,您需要缓存最后发送的请求( shouldStartLoadWithRequest:委托方法将是一个很好的地方),因为UIWebView的request属性似乎总是返回最后一个成功的请求(尽管documentation没有明确规定这一点,所以我不认为这是一个缺陷)。
https://stackoverflow.com/questions/13299782
复制相似问题