Custom NSURLProtocol not used in second webview

964 views Asked by At

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!

1

There are 1 answers

0
Alfie Hanssen On

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.