NSURLProtocolをオーバーライドし、特定のstatusCodeでHTTP応答を返す必要があります。 NSHTTPURLResponseはstatusCodeをセッターを持っていないので、私はそれを上書きしようとした:NSURLResponseでstatusCodeを設定するには
@interface MyHTTPURLResponse : NSHTTPURLResponse {}
@implementation MyHTTPURLResponse
- (NSInteger)statusCode {
return 200; //stub code
}
@end
NSURLProtocolの
オーバーライドstartLoading方法は、次のようになります。
-(void)startLoading
{
NSString *url = [[[self request] URL] absoluteString];
if([url isEqualToString:SPECIFIC_URL]){
MyURLResponse *response = [[MyURLResponse alloc] initWithURL:[NSURL URLWithString:@"http://fakeUrl"]
MIMEType:@"text/plain"
expectedContentLength:0 textEncodingName:nil];
[[self client] URLProtocol:self
didReceiveResponse:response
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[[self client] URLProtocol:self didLoadData:[@"Fake response string"
dataUsingEncoding:NSASCIIStringEncoding]];
[[self client] URLProtocolDidFinishLoading:self];
[response release];
}
else{
[NSURLConnection connectionWithRequest:[self request] delegate:self];
}
}
しかし、このアプローチは動作しません、NSURLProtocolで作成した応答Webページでは常にstatusCode = 0です。 NSURLConnectionによってネットワークから返される応答は、通常、正常なexpected statusCodesを持ちます。
誰でも作成したNSURLResponseに対してstatusCodeを明示的に設定する方法を教えてください。 〜
この問題の解決方法をお探しですか? – TomSwift