如何从NSURLProtocol内部获取请求的UIWebView [英] How to get requesting UIWebView from inside of NSURLProtocol
问题描述
我的应用程序使用NSURLProtocol的子类.应用程序中有多个UIWebView,对于在NSURLProtocol中实现的特定算法,我需要知道哪个UIWebView发送请求.
My app uses subclassing of NSURLProtocol. There are several UIWebViews in the app and for specific algorithm implemented in NSURLProtocol I need to know which one of the UIWebViews sends the request.
我的理解是[自我客户]返回的对象应与请求对象有所联系.但是,NSURLProtocolClient(即由[自身客户端]返回的对象实现的协议)和基础对象_NSCFURLProtocolBridge都没有任何属性/方法来获取请求的发送者.
My understanding is that object returned by [self client] should be somewhat connected with requesting object. But neither NSURLProtocolClient (that is the protocol implemented by object returned by [self client]) nor underlying object _NSCFURLProtocolBridge have any properties/methods to get the sender of the request.
有人可以帮助我提出建议吗?
Can anyone help me with ideas?
推荐答案
NSURLRequest
具有称为mainDocumentURL
的方法,该方法返回根文档的URL.您可以像这样在UIWebViewDelegate
方法中将其保存下来,
NSURLRequest
has a method called mainDocumentURL
which returns the URL of the root document. You can possibly save that away in the UIWebViewDelegate
method like this,
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if([[request.URL absoluteString] isEqualToString:[request.mainDocumentURL absoluteString]])
{
// associate this URL with this UIWebview
}
}
然后可以在NSURLProtocol
方法中查看mainDocumentURL
以标识UIWebView
.这并非万无一失,因为它不能解决多个UIWebViews
加载相同URL的情况.但这是我能想到的最佳解决方案.
You can then look at the mainDocumentURL
in your NSURLProtocol
methods to identify the UIWebView
. This is not fool proof since it doesn't account for cases where multiple UIWebViews
load the same URL. But this is the best solution I could think of.
这篇关于如何从NSURLProtocol内部获取请求的UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!