自定义 NSURLProtocol 未在第二个 Web 视图中使用
我已经对 NSURLProtocol 进行了子类化,并使用以下代码在 didFinishLaunchingWithOptions
中注册了它:
+ (void) registerProtocol
{
static BOOL registered = NO;
if (!registered)
{
[NSURLProtocol registerClass:[MyURLProtocol class]];
registered = YES;
}
}
对于我的应用程序(在主窗口中)中的第一个 UIWebView
方法 < code>canInitWithRequest 被触发,我可以执行我的自定义代码。
不过,我有第二个 UIWebView,它位于 UIViewController 内,它在应用程序中的某个时刻推送(以模态方式呈现)。第二个 UIWebView 不会调用 canInitWithRequest ,因此我无法执行自定义代码。当在创建 UIWebView
的两个实例之后注册协议时,情况也是如此。
有人知道为什么吗?
[编辑] 哦!我刚刚发现这只是第二个 webview 中加载的 javascript 中的一个简单错误:( 现在在两个网络视图中都像魅力一样!
I've subclassed NSURLProtocol
and registered it in didFinishLaunchingWithOptions
with this code:
+ (void) registerProtocol
{
static BOOL registered = NO;
if (!registered)
{
[NSURLProtocol registerClass:[MyURLProtocol class]];
registered = YES;
}
}
For the first UIWebView
in my app (in the mainwindow) the method canInitWithRequest
is triggered, and I can execute my custom code.
However I have a second UIWebView
, that is inside an UIViewController
which is pushed at some point in the app (presented modally). The canInitWithRequest
will NOT be called for the second UIWebView
, thus I cannot execute my custom code. This is even true when the protocol is registered after both instances of UIWebView
are created.
Anyone knows why?
[edit]
d'oh! i just found it was just a plain error in the javascript that is loaded in the second webview :(
works like a charm in both webviews now!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这是否与您的情况和解决方案有关,但发布以供记录。今天经过多次尝试和磨难后发现,如果您希望多次发送相同的 url,则 ajax 调用将消息发送到目标 c(该消息将被 NSURLProtocol 的子类拦截)应该将缓存设置为 false。像这样:
如果cache = true,那么objective c将永远不会收到同一url上的后续请求。
Not sure if this is related to your situation and solution, but posting for the record. Discovered today after much trial and tribulation that an ajax call sending a message to objective c which is to be intercepted by a subclass of NSURLProtocol should have cache set to false if you expect to be sending the same url more than once. Like so:
If cache = true then objective c will never receive subsequent requests on the same url.