【问题标题】:iOS5 NSURLConnection methods deprecated不推荐使用 iOS5 NSURLConnection 方法
【发布时间】:2011-12-13 07:43:39
【问题描述】:

我正在尝试编写一个 iOS 应用程序,它发出异步请求以通过网络获取数据。似乎很多人为此推荐使用 NSURLConnection,并且经常提到委托方法 connection:didReceiveData:。

不幸的是,我终其一生都无法找出这个委托方法的文档记录在哪里。一方面,它不在the protocol reference for NSURLConnectionDelegate 中。它在the NSURLConnection Class Reference 中列出,但显然在 iOS5 中已被弃用。该文档没有解释它被弃用的原因,或者开发人员应该使用什么来实现类似的功能。

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

谢谢

【问题讨论】:

标签: ios ios5


【解决方案1】:

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

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

【讨论】:

  • 如果我们打算支持iOS4,我们是否应该在检查iOS版本之后才包含这个委托?
  • @aiham 不知道,对不起。如果方法签名相同,我认为这不是问题,但我没有办法验证这一点。
  • 这应该不是问题,不。
  • 它现在在文档中:developer.apple.com/library/ios/#documentation/Foundation/…。它应该适用于 iOS > 3.0
【解决方案2】:

文档确实是一团糟,尽管查看 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,请注意如果这些方法存在于您的委托中,您的 NSURLConnectionDataDelegate 方法将不会被调用。

我在打开目标 URL 时也遇到了问题,iOS 一直告诉我该文件不存在,尽管文档表明该文件在方法调用期间保证存在。

【讨论】:

【解决方案3】:

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

NSURLConnection 上的文档,如所写,让你兴奋不已。

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

我能够拼凑起来的真实故事是,以前在NSURLConnectionProtocol 中的大多数方法都已移至名为NSURLConnectionDataProtocol 的新协议中。不幸的是,这个新协议要么没有记录,要么没有索引,所以你找不到它。这相当于同一件事。)

在其他有趣的新闻中,显然有一个名为NSURLConnectionDownloadDelegate 的新协议。听起来 iOS 的 NSURLConnection 正在添加一些在 MacOS 中的 NSURLDownload 中可用的功能。 NSURLConnectionDownloadDelegate 协议已记录在案。

【讨论】:

  • 我完全同意。这个文档一塌糊涂,iOS5.0官方SDK中的URL Loading编程指南不是最新的。我们将继续使用旧协议...
  • 根据我在 Apple Developer Forums 中阅读的内容,目前 NSURLConnectionDownloadDelegate 仅与 NK(Newsstand Kit)类一起工作。显然,Apple 已经记录了一个错误,最终它可能会与 NSURLConnection 一起使用。
【解决方案4】:

您会认为从 NSURLConnection 弃用这些方法需要某种解释器,但我还没有找到。到目前为止我能做的最好的是来自 Apple 的URL Loading System Programming Guide

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

这意味着一个非正式的协议。

【讨论】:

    猜你喜欢
    • 2015-12-03
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2012-02-22
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多