1

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!

user826955
  • 3,137
  • 2
  • 30
  • 71
  • The problem is not in registering your protocol. You only need to register it once. I would be curious to see how you are loading your requests in the UIWebView. For example, are you calling [webView loadRequest:request]. – jimmyg Jan 11 '12 at 23:14
  • actually the request is fired from a javascript/ajax call, and i'm trying to intercept it in canInitWithRequest. works for the first webview, does not work for the second webview :( – user826955 Jan 12 '12 at 07:15
  • Can you provide some detail on the fix you discovered? Even if it's a js error that's specific to your App. I'm encountering the very same issue and am pulling my hair out trying to figure out the problem. – Alfie Hanssen Nov 02 '12 at 14:35

1 Answers1

2

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.

Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74