iOS5 NSURLConnection方法已弃用



我正在尝试编写一个iOS应用程序,使异步请求通过网络获取数据。似乎很多人都建议使用NSURLConnection来做这个,并且经常提到委托方法connection:didReceiveData:.

不幸的是,我无法找到这个委托方法的文档在哪里。首先,它不在NSURLConnectionDelegate的协议引用中。它在NSURLConnection类参考中列出,但显然在iOS5中已被弃用。文档没有解释为什么它被弃用,或者开发人员应该使用什么来实现类似的功能。

我错过了什么?我读到的很多内容似乎都暗示iOS5的NSURLConnection有很大的变化。这些更改记录在哪里?委托方法的文档是否完整?

谢谢

在头文件周围查找告诉我,这些方法是从一个非正式的协议(这是一个已弃用的Obj-C模式)移动到一个名为NSURLConnectionDataDelegate的正式委托协议,该协议位于NSURLConnection.h中,但没有公共文档。

文档的其余部分像以前一样继续使用这些方法,所以我猜测这是文档中的遗漏。也就是说,这些方法(大部分)不会去任何地方,它们只是被重组成几个协议,文档团队一直在懈怠。尝试使您的委托对象符合适当的协议,并使用头文件中的签名实现方法。

文档确实是一团糟,尽管看看NSURLConnection.h从4.3到5.0的更改日志:

删除
Removed -[NSObject connection:canAuthenticateAgainstProtectionSpace:]
Removed -[NSObject connection:didCancelAuthenticationChallenge:]
Removed -[NSObject connection:didFailWithError:]
Removed -[NSObject connection:didReceiveAuthenticationChallenge:]
Removed -[NSObject connection:didReceiveData:]
Removed -[NSObject connection:didReceiveResponse:]
Removed -[NSObject connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:]
Removed -[NSObject connection:needNewBodyStream:]
Removed -[NSObject connection:willCacheResponse:]
Removed -[NSObject connection:willSendRequest:redirectResponse:]
Removed -[NSObject connectionDidFinishLoading:]
Removed -[NSObject connectionShouldUseCredentialStorage:]
Removed NSObject(NSURLConnectionDelegate)
添加

Added -[NSURLConnection currentRequest]
Added -[NSURLConnection originalRequest]
Added +[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]
Added -[NSURLConnection setDelegateQueue:]
Added NSURLConnectionDataDelegate
Added -[NSURLConnectionDataDelegate connection:didReceiveData:]
Added -[NSURLConnectionDataDelegate connection:didReceiveResponse:]
Added -[NSURLConnectionDataDelegate connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:]
Added -[NSURLConnectionDataDelegate connection:needNewBodyStream:]
Added -[NSURLConnectionDataDelegate connection:willCacheResponse:]
Added -[NSURLConnectionDataDelegate connection:willSendRequest:redirectResponse:]
Added -[NSURLConnectionDataDelegate connectionDidFinishLoading:]
Added NSURLConnectionDelegate
Added -[NSURLConnectionDelegate connection:canAuthenticateAgainstProtectionSpace:]
Added -[NSURLConnectionDelegate connection:didCancelAuthenticationChallenge:]
Added -[NSURLConnectionDelegate connection:didFailWithError:]
Added -[NSURLConnectionDelegate connection:didReceiveAuthenticationChallenge:]
Added -[NSURLConnectionDelegate connection:willSendRequestForAuthenticationChallenge:]
Added -[NSURLConnectionDelegate connectionShouldUseCredentialStorage:]
Added NSURLConnectionDownloadDelegate
Added -[NSURLConnectionDownloadDelegate connection:didWriteData:totalBytesWritten:expectedTotalBytes:]
Added -[NSURLConnectionDownloadDelegate connectionDidFinishDownloading:destinationURL:]
Added -[NSURLConnectionDownloadDelegate connectionDidResumeDownloading:totalBytesWritten:expectedTotalBytes:]
Added NSURLConnection(NSURLConnectionQueuedLoading)

所以看起来这些函数确实还在。只要把协议添加到你的@interface声明中,就可以了。

我尝试了新的NSURLConnectionDownloadDelegate,被警告如果这些方法存在于你的delegate中,你的NSURLConnectionDataDelegate方法将不会被调用。

我也有打开destinationURL的问题,iOS一直告诉我文件不存在,尽管文档表明该文件在方法调用期间保证存在。

文档是一个@$@#$乱。这才是真实的故事。

关于NSURLConnection的文档,正如所写的,让你陷入困境。

文档的第一部分告诉您使用NSURLConnection协议(如connection:didReceiveData:)中的各种方法来处理传入的数据。如果您在概述中单击任何这些方法,它将引导您到一个DEPRECATED methods列表!)

我能够拼凑起来的真实故事是,以前在NSURLConnectionProtocol中的大多数方法已经转移到一个名为NSURLConnectionDataProtocol的新协议中。不幸的是,这个新协议要么没有文档,要么没有索引,因此您无法找到它。)

在其他有趣的新闻中,显然有一个名为NSURLConnectionDownloadDelegate的新协议。听起来iOS版NSURLConnection添加了MacOS版NSURLDownload的一些功能。NSURLConnectionDownloadDelegate协议有文档

您可能会认为从NSURLConnection中弃用这些方法需要某种解释,但我还没有找到。到目前为止,我能做的最好的是苹果的URL加载系统编程指南:

为了下载URL的内容,应用程序需要提供一个委托对象,至少实现以下委托方法:connection:didReceiveResponse:, connection:didReceiveData:, connection:didFailWithError:connectionDidFinishLoading:

表示非正式协议

相关内容

最新更新