返回自定义NSURLResponse类

时间:2011-04-08 15:43:16

标签: iphone objective-c nsurlconnection nsurlprotocol

我尝试在我的自定义NSURLProtocol中返回NSURLResponse的子类的对象,但似乎返回的响应总是由普通的NSURLResponse交换。

这是我返回自定义TestURLResponse的NSURLProtocol的一部分:

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];

当我阅读使用此协议的内容时,我希望得到TestURLResponse类型的响应,但在下面的代码中我总是得到一个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];

整个测试项目可以在

下载

http://ul.to/9yylk65s

现在我不知道我做错了什么或是否有什么东西坏了。 NSURLProtocol的文档声明

  

协议实现者可能希望创建一个自定义的可变NSURLResponse类来提供协议特定信息。

但在我看来,这根本不起作用。

0 个答案:

没有答案