我检查了 stackOverFlow 和其他,在didReceiveData:, didReceiveResponse:
NSURLConnectionDataDelegate 版本 4.3 上可用。从 2.3 版到 4.3 版,它们在 NSURLConnectionDelegate 中可用。
看看这些:
http://stackoverflow.com:(10937215)connection-didreceivedata-is-not-call
http://stackoverflow.com:(7862316) ios5 nsurlconnection 方法已弃用
通过检查 SDK 版本,我猜我们可以继承一个特定的类,我还没有测试过。但是我猜是可以实现的。
但问题是我在 iOS 6.0 和 3G 网络上运行,然后它一直因超时错误而失败。在 WIFI 上,didReceiveData:
即使我使用 NSURLConnectionDelegate 也会被调用。感觉有点奇怪。
在我扩展 NSURLConnectionDelegate 的类中,这些是要查看的方法。
-(void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Private didReceiveResponse - %d", [response statusCode]);
if([response statusCode] == 200)
connectionstatus = YES;
}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
{
self.connectionstatus = YES;
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
{
NSLog(@"Private connectionDidFinishLoading");
self.connectionstatus = NO;
[self reinitConnection];
}
-(void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error
{
self.connectionstatus = NO;
self.connection= nil;
}
- (void) reinitConnection
{
self.connectionstatus = YES;
connectionCount++;
NSString* urlS = @“VALID url”; //Such as http://google.com
NSURL *url = [NSURL URLWithString:urlS];
self.req = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
NSString *appstring =[httpreq appendMyParamsPost:req];
[req setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",[url absoluteString],appstring]]];
NSString* authValue = @“SomeValue”,
[req addValue:@"application/vnd.gupshup.privatechat-v1+json" forHTTPHeaderField:@"Accept"];
[req setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:authValue forHTTPHeaderField:@"Authorization"];
[req setHTTPMethod:@"GET"];
[self.connection cancel];
self.connection = nil;
self.connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
}
有什么问题吗?为什么它专门在 3G 上,虽然在 SDK 级别存在方法 shuffle 的问题,在哪里,它也必须在 WIFI 上发生?
提前致谢。文卡塔劳