Return custom NSURLResponse class

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.

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

上一篇: 如何为我的UIWebView所做的每个请求添加自定义标题字段

下一篇: 返回自定义的NSURLResponse类