自定义 NSURLProtocol 未在第二个 Web 视图中使用

发布于 2024-12-26 05:17:46 字数 674 浏览 4 评论 0原文

我已经对 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 NSURLProtocoland 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2025-01-02 05:17:46

不确定这是否与您的情况和解决方案有关,但发布以供记录。今天经过多次尝试和磨难后发现,如果您希望多次发送相同的 url,则 ajax 调用将消息发送到目标 c(该消息将被 NSURLProtocol 的子类拦截)应该将缓存设置为 false。像这样:

$.ajax({
       url:"atavistcommand://" + name,
       data:JSON.stringify(data),
       type:"GET",
       processData:false,
       cache: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:

$.ajax({
       url:"atavistcommand://" + name,
       data:JSON.stringify(data),
       type:"GET",
       processData:false,
       cache:false )};

If cache = true then objective c will never receive subsequent requests on the same url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文