如何从NSURLProtocol内部获取请求的UIWebView [英] How to get requesting UIWebView from inside of NSURLProtocol

查看:65
本文介绍了如何从NSURLProtocol内部获取请求的UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用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屋!

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