NSURLConnection 方法在 IOS5 中不再可用
我正在研究 NSURLConnection 类,它可用于建立到 URL 的同步或异步连接,然后检索其数据...IOS 5 和 中对此类进行了很多更改我已经看到他们引入了一些与身份验证或下载相关的正式协议,但我没有看到,例如,是否 connection:didReceiveResponse:
消息(之前发送给委托并且它不再可用)在某些地方仍然可用协议.. 如何实现异步连接并在收到响应后立即检索,例如 HTTP 标头?我确信有一种方法比使用 NSURLConnection
和 connection:didReceiveResponse:
消息更好。像 stringWithContentsOfURL
这样的方法总是加载内容同步?您使用什么来在应用程序中实现异步下载,避免不推荐使用的方法并对诸如_http响应收到_m等事件做出反应?如果可能的话,您是否在后台任务中启动同步下载?
I was looking at the NSURLConnection
class which could be used to establish a sync or async connection to an URL and then retrieve its data... a lot of changes have been made to this class with IOS 5 and I've seen they introduced some formal protocols related to authentication or download, but I don't see, for example, if the connection:didReceiveResponse:
message (that was previously sent to the delegate and that it is no more available) is still available in some protocols.. How do you implement an async connection and retrieve, for example, HTTP headers as soon as the Response is received? I'm sure there is a way better than using NSURLConnection
along with the connection:didReceiveResponse:
message.. methods like stringWithContentsOfURL
do always load content synchronously? What do you use to implement async downloads in your apps avoiding deprecated methods and reacting to events such as _http response received_m etc ? Do you launch synchronous downloads in background tasks, if possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSURLConnectionDelegate
已成为正式协议(在之前的版本中是非正式协议)。在此协议中,声明了以下(未弃用的)方法:connection:didFailWithError:
connectionShouldUseCredentialStorage:
connection:willSendRequestForAuthenticationChallenge:
此外,还有两个符合
NSURLConnectionDelegate
的子协议:NSURLConnectionDataDelegate
用于委托将数据加载到内存,并声明以下方法,我相信其中一些方法您会熟悉:connection:willSendRequest:redirectResponse:
connection:didReceiveResponse:
connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
connection:willCacheResponse:
connectionDidFinishLoading:
NSURLConnectionDownloadDelegate
用于存储数据的委托直接写入磁盘文件,并声明以下方法:connection:didWriteData:totalBytesWritten:expectedTotalBytes:
connectionDidResumeDownloading:totalBytesWritten:expectedTotalBytes:
connectionDidFinishDownloading:destinationURL:
如您所见,您仍然可以使用之前的版本代表们,可能会进行一些细微的修改。
有关详细信息,请参阅 iOS 4.3 到 iOS 5.0 API 差异文档 和 NSURLConnection.h在本地 Xcode 安装中。当新的 SDK 版本发布时,头文件中的文档比开发人员库中提供的文档更可靠的情况并不少见。后者需要一段时间才能更新。
NSURLConnectionDelegate
has become a formal protocol (it was an informal protocol in previous versions). In this protocol, the following (non-deprecated) methods are declared:connection:didFailWithError:
connectionShouldUseCredentialStorage:
connection:willSendRequestForAuthenticationChallenge:
Furthermore, there are two subprotocols that conform to
NSURLConnectionDelegate
:NSURLConnectionDataDelegate
is used for delegates that load data to memory, and declares the following methods, some of which I’m sure you’ll find familiar:connection:willSendRequest:redirectResponse:
connection:didReceiveResponse:
connection:didReceiveData:
connection:needNewBodyStream:
connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
connection:willCacheResponse:
connectionDidFinishLoading:
NSURLConnectionDownloadDelegate
is used for delegates that store data directly to a disk file, and declares the following methods:connection:didWriteData:totalBytesWritten:expectedTotalBytes:
connectionDidResumeDownloading:totalBytesWritten:expectedTotalBytes:
connectionDidFinishDownloading:destinationURL:
As you can see, you can still use your previous delegates, possibly with some minor modifications.
For more information, see the iOS 4.3 to iOS 5.0 API Differences document and NSURLConnection.h in your local Xcode installation. When a new SDK version is released, it’s not uncommon for the documentation inside the header files to be more reliable than the documentation available on the developer library. It takes a while for the latter to be up-to-date.
我刚刚遇到了同样的问题。看起来使用块和 NSOperationQueue 发送异步请求更加简单。
这意味着委托现在仅用于身份验证和失败问题。
I just encountered this same issue. Looks like sending an asynchronous request is more simplified with blocks and
NSOperationQueue
.This means that the delegate is now only used for authentication and failure issues.
不!
如果您仔细查看 Apple 的库,它们不仅限于用于身份验证和失败问题。
自从将
+(void)sendAsynchronousRequest:queue:completionHandler:
引入 NSConnection 类对象后,许多可以执行与以前一样多的NSConnectionDelegate
方法的东西现在可以在正式协议中使用,称为“NSConnectionDataDelegate
” &NSConnectionDownloadDelegate
,打开一个新房间,为NSURLConnection
方法添加更多功能。 (从iOS5开始)所以我认为这是一种改进,而不是限制它们的使用。
NO!
They are NOT limited to use for authentication and failure issues if you look carefully through the Apple's library.
Since introducing
+(void)sendAsynchronousRequest:queue:completionHandler:
to NSConnection class object, Many things which can perform as manyNSConnectionDelegate
method as before can now be used in formal protocols called "NSConnectionDataDelegate
" &NSConnectionDownloadDelegate
, opening a new room to add more feature toNSURLConnection
methods. (from iOS5 on)So I think it is an improvement, not limiting their use.
即使我还没有在苹果网站上找到文档
https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html
它应该可以在这里找到
Even I havent found the documentation on the Apple website
https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html
It should have been available over here