已弃用的NSURLConnection方法,还有其他选择吗?

时间:2012-02-28 14:12:27

标签: iphone objective-c nsurlconnection

我正在设置一个NSURLConnection来访问远程服务器:

NSURL *url = [[NSURL alloc] initWithString:projectURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

使用以下代理方法:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse

所有这些方法都说“在iOS 2.0至iOS 4.3中可用”。我的问题应该是针对当前的iOS5做另一种方式吗?我需要异步传输,需要通过NSURLCredential进行UID / PWD身份验证。

编辑:

因此,如果我在我的标题中采用协议<NSURLConnectionDelegate>并使用上面的委托方法,我会以正确的方式执行此操作吗?

1 个答案:

答案 0 :(得分:4)

对于iOS 5,看起来您可以利用新的NSURLConnectionDelegate protocol(为您链接的文档)。

And here's a related (or maybe even duplicate?) question that explains even better