iOS5 NSURLConnection methods deprecated

Solution 1:

Fishing around the header files tells me that the methods were moved from an informal protocol (which is a deprecated Obj-C pattern) into a formal delegate protocol called NSURLConnectionDataDelegate that's in NSURLConnection.h, but doesn't have a public documentation.

The rest of the documentation keeps using the methods as before, so my guess is this is an omission in the documentation. I.e. the methods (mostly) aren't going anywhere, they were just reshuffled into several protocols and the documentation team has been slacking off. Try making your delegate object conform to the appropriate protocol, and implement the methods with the signatures from the header file.

Solution 2:

Documentation is indeed a mess, although looking at the changelog from 4.3 to 5.0 for NSURLConnection.h:

Removed

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

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)

So it seems those functions are indeed still there. Just add the protocols to your @interface declaration and you should be good to go.

I tried the new NSURLConnectionDownloadDelegate, be warned that if these methods are present in your delegate, your NSURLConnectionDataDelegate methods will NOT be called.

I also had issue opening the destinationURL, iOS kept telling me the file did not exist although the documentation indicates the file is guaranteed to exist during the method call.

Solution 3:

The documentation is a @$@#$ mess. That's the real story.

The Documentation on NSURLConnection, as written, leaves you high and dry.

The first part of the documentation tells you to use various methods in the NSURLConnection protocol (like connection:didReceiveData:) to handle incoming data. If you click on any of those methods in the overview, it leads you to a list of DEPRECATED METHODS!)

The real story that I've been able to piece together is that most of the methods formerly in NSURLConnectionProtocol have been moved to a new protocol called NSURLConnectionDataProtocol. Unfortunately, that new protocol is either not documented, or it's not indexed, so you can't find it. Which amounts to the same thing.)

In other interesting news, there is apparently a new protocol called NSURLConnectionDownloadDelegate. It sounds like NSURLConnection for iOS is adding some of the functionality that is available in NSURLDownload in MacOS. The NSURLConnectionDownloadDelegate protocol IS documented.