Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios didFailLoadWithError中的webVIew.request.URL:上一个URL不是失败的URL吗_Ios_Uiwebview - Fatal编程技术网

Ios didFailLoadWithError中的webVIew.request.URL:上一个URL不是失败的URL吗

Ios didFailLoadWithError中的webVIew.request.URL:上一个URL不是失败的URL吗,ios,uiwebview,Ios,Uiwebview,假设有一个网页a.html包含到B.html的链接 如果单击了B.html,则shouldStartLoadWithRequest:中的request.URL将按原样为B.html。但是,如果加载该页面时出现问题(例如,假设该页面不存在),则在didFailLoadWithError中:webView.request.URL的值不是B.html,而是a.html 因此,除非缓存最后一次页面加载,否则不可能知道哪个页面加载失败,但我希望webView.request.URL是B.html,因此这是

假设有一个网页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]

您是对的,您需要缓存最后发送的请求(
shouldStartLoadWithRequest:
委托方法将是一个很好的地方),因为
UIWebView
请求
属性似乎总是返回最后成功的请求(虽然没有特别指定,所以我不会称之为缺陷)。

我也有同样的问题。如果其他人也这样做,则error.userInfo dictionary会起作用

-(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
文档中说,iOS4中不推荐使用NserErrorFailingUrlStringKey(仅提供向后兼容性),您应该改用NserErrorFailingUrlStringErrorKey


但是,NserErrorFailingUrlStringErrorKey不会返回(无论如何,我的UIWebView版本不会返回)。相反,NserErrorFailingUrKey是另一个返回URL的键,但我在文档中的任何地方都找不到它。

我在Swift 3.1中遇到了同样的问题。要获取失败的URL,请使用此委托方法:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool{

    let failedUrl = (request.url?.absoluteString)! as String
    return true
}

你能帮我吗?我面临同样的问题,但在使用你的代码后没有成功。