假设有一个网页 A.html 包含一个指向 B.html 的链接。
如果单击 B.html,则 shouldStartLoadWithRequest: 中的 request.URL 将是 B.html,因为它应该是。但是,如果加载该页面时出现问题(假设它不存在),则在 didFailLoadWithError 中:webView.request.URL 的值不是 B.html,而是 A.html。
因此,除非我缓存最后一个页面加载,否则似乎不可能知道哪个页面加载失败,但我希望 webView.request.URL 是 B.html,因此这是一个缺陷吗? 我没有看到关于它应该是什么的文档。
[iOS 6]
最佳答案
我遇到了同样的问题。如果其他人也这样做,则 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
doco 说 NSErrorFailingURLStringKey 在 iOS4 中被弃用(仅为向后兼容而提供),您应该改用 NSURLErrorFailingURLStringErrorKey。
但是 NSURLErrorFailingURLStringErrorKey 没有被返回(反正我的 UIWebView 版本没有)。相反,NSErrorFailingURLKey 是另一个返回 URL 的键,但我无法在任何地方的文档中找到它。
关于ios - webVIew.request.URL 在 didFailLoadWithError : is the previous URL not the failed URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13299782/