Ios5 Nsurlconnection Methods Deprecated

iOS5 NSURLConnection methods deprecated

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.

Deprecated NSURLConnection Methods, is there an alternative?

For iOS 5, looks like you can take advantage of the new NSURLConnectionDelegate protocol (documentation linked for you).

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

NSURLConnection methods no more available in IOS5

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.

NSURLConnection deprecated in iOS9

Now you have to use NSURLSession

Example (GET):

-(void)placeGetRequest:(NSString *)action withHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))ourBlock {

NSString *urlString = [NSString stringWithFormat:@"%@/%@", URL_API, action];

NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:ourBlock] resume];
}

Now you will need to call that method with an action (or your full URL if you prefer) and the block that will be executed when the API call return.

[self placeGetRequest:@"action" withHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// your code
}];

Inside that block, you will received a NSData with the response data and NSURLResponse with the HTTP response. So now, you can put your code there:

NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
documentsURL = [documentsURL URLByAppendingPathComponent:@"localFile.pdf"];

[data writeToURL:documentsURL atomically:YES];

Main difference between NSURLSession and NSURLConnection

  • NSURLConnection: if we have an open connection with NSURLConnection and the system interrupt our App, when our App goes to background mode, everything we have received or sent were lost.
    Process diagram for NSURLConnection

  • NSURLSession: solve this problem and also give us out of process downloads. It manage the connection process even when we don't have access. You will need to use application:handleEventsForBackgroundURLSession:completionHandler in your AppDelegate
    Process diagram for NSURLSession

So with the use of NSURLSession, you don't need to manage or to check
your internet connection because OS does it for you.

NSURLConnection initWithRequest is deprecated

It seems that the whole NSURLConnection API has been deprecated in iOS 9. Existing apps will continue to work, but new builds (linked against iOS SDK) must use the newer NSURLSession API.

Ray Wenderlich has a good tutorial here.
Also, of course, check the official documentation.

What is the situation regarding the NSURLConnection timeout with iOS5?

In my experience I have found that:
- Its possible to set a timeout value of anything (i.e. 0.1 seconds) for HTTP GETS.
- The OS does impose a limit of 240 for HTTP POSTS with a body.

use deprecated methods in iOS 5

  1. You will probably not have a problem on iOS 5 devices using methods that the compiler is telling you are deprecated. Of course, it would be a good thing to clear up this issue over time because deprecated means those methods might not be present in iOS 6 (or whatever it will be called).

    • What you need to watch out for are compiler warnings or errors that an object may not respond to a particular method call. These would occur for methods that actually were removed along the way. It sounds like you have already done this, but be sure that you have set your SDK set to iOS 5 in XCode to ensure you are getting all the errors/warnings. But the lesson here is test, test, test (on devices as well).
  2. As long as your code compiles and runs against iOS 5, using a deprecated method alone probably won't get your app disqualified. If it causes any crashing or anything like that during testing, however, they will likely kick it back to you for rework.



Related Topics



Leave a reply



Submit