NSURLProtocol'u geçersiz kılar ve belirli bir statusCode ile HTTP yanıtını döndürmeliyim. NSHTTPURLResponse statusCode ayarlayıcı yok, bu yüzden bu değeri geçersiz kılabilir denedi:NSURLResponse durumunda durum kodu nasıl ayarlanır
@interface MyHTTPURLResponse : NSHTTPURLResponse {}
@implementation MyHTTPURLResponse
- (NSInteger)statusCode {
return 200; //stub code
}
@end
NSURLProtocol ait
Bastırıldı startLoading yöntemi aşağıdaki gibidir:,
-(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];
}
}
Ama bu yaklaşım çalışmaz NSURLProtocol oluşturulan tepkiyi Web sayfasındaki statusCode = 0 ile her zaman. Aynı zamanda, NSURLConnection tarafından ağdan döndürülen yanıtların normal beklenen statusCodes'ları vardır.
Oluşturulan NSURLResponse için statusCode'u açıkça nasıl belirleyeceğiniz konusunda fikir sahibi olan herkes yardımcı olabilir mi? Teşekkürler.
Bu soruna bir çözüm buldunuz mu? – TomSwift