Custom NSURLProtocol not used in second webview

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!


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.

链接地址: http://www.djcxy.com/p/5926.html

上一篇: 使用具有UIWebView和POST请求的自定义NSURLProtocol

下一篇: 自定义NSURLProtocol不在第二个webview中使用