5

I try to return an object of a subclass of NSURLResponse in my custom NSURLProtocol, but it seems the returned response is always exchanged by a normal NSURLResponse.

This is the part of the NSURLProtocol where I return my custom TestURLResponse:

TestURLResponse* response = [[[TestURLResponse alloc] initWithURL: [[self request] URL]
                                                        MIMEType: @"text/plain" 
                                           expectedContentLength: 4
                                                textEncodingName:@"UTF-8"]
                             autorelease];
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[self.client URLProtocol:self didLoadData: [@"Test" dataUsingEncoding:NSUTF8StringEncoding]];
[self.client URLProtocolDidFinishLoading: self];

When I read something with this protocol I expect to get the response of type TestURLResponse, but in the following code I always get a NSURLResponse:

    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"test://test"]];
NSURLResponse* response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

NSString *className = NSStringFromClass([response class]);

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:className  message:className delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];

The whole test project can be downloaded at

http://ul.to/9yylk65s

Right now I have no idea if I'm doing something wrong or if there is something broken. The documentation of NSURLProtocol states

A protocol implementor may wish to create a custom, mutable NSURLResponse class to provide protocol specific information.

but it seems to me, this isn't working at all.

cemonds
  • 225
  • 1
  • 9
  • 1
    I'm very confused by this. I downloaded your project and tried a few things, like implementing `-(id)initWithCoder:` and `-(id)copy` / `-(id)copyWithZone:` but nothing helped. The object is also never retained when passed to the url protocol client. – Alfonso Apr 08 '11 at 16:06
  • Also played around with your sample code, but didn't get anywhere. Since an entirely different response object is being sent to the connection delegate, I figured maybe it had to do with NSURLResponse's default `-copyWithZone:` behavior, but it's not even being called. Maybe this is a bug in Apple's NSURLProtocolClient implementation? – Cameron Spickert Apr 08 '11 at 16:29
  • Thanks for your comments. I tried the whole day to find the place, where the NSURLResponse is exchanged, but I didn't find anything. Right now I also think this could be a bug in the apple implementation or in their documentation. Maybe I should ask in the apple developer forums and try to find some one from apple who can explain this. – cemonds Apr 08 '11 at 21:49
  • I filed a bug at apple with this issue, since also in the apple developer forums no one found something wrong about this. If I find a solution or workaround, I add it here. – cemonds Apr 19 '11 at 14:01

0 Answers0