将 NSURLRequest 与 UIWebView 关联起来 [英] Correlate NSURLRequest with UIWebView

查看:47
本文介绍了将 NSURLRequest 与 UIWebView 关联起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 中,我希望能够关联 NSURLRequest--any NSURLRequest,无论是导航请求还是图像或样式表请求——带有发送它的 UIWebView.这似乎要求实现一个 NSURLProtocol 子类来捕获和分析通过的请求.但是,我找不到将请求与其 Web 视图相关联的方法,而且 NSURLProtocol 似乎只能理解 NSURLRequests.

In iOS, I'd like to be able to associate an NSURLRequest--any NSURLRequest, whether a navigation request or image or stylesheet request--with the UIWebView that sent it. This seems to call for the implementation of an NSURLProtocol subclass to capture and analyze requests as they come through. However, I can't find a way to correlate a request with its web view, and NSURLProtocol only seems to understand NSURLRequests.

这适用于导航请求,因为我可以在我的 UIWebViewDelegate 中进行关联.但这不适用于不触发 UIWebViewDelegate 方法的辅助资源请求,例如图像.

This would be fine for navigation requests, as I could make the association in my UIWebViewDelegate. But this won't work for secondary resource requests such as images, which do not trigger UIWebViewDelegate methods.

有人对建立这种关联的最佳方式有什么想法吗?

Anyone have any thoughts as to the best way to make this association?

推荐答案

在您的自定义协议中,也会为引用的资源调用 canUseProtocol 委托方法.您也许可以利用这个钩子来链接您的请求,但我不确定您是否可以在不取消该请求并使用 url 执行单独的请求的情况下获得失败回调.

In your custom protocol, canUseProtocol delegate method gets called for referenced resources as well. You may be able to leverage this hook to link your requests, but I am not sure if you can get a failure callback without cancelling that request and performing a separate one with the url.

你关于引用资源的问题让我有了这个发现,这让我解决了我遇到的缓存问题.

Your question about referenced resources led me to that discovery, which allowed me to solve a caching problem i was having.

谢谢,我希望这有助于回报!

Thank you and I hope this helps in return!

这篇关于将 NSURLRequest 与 UIWebView 关联起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆