Downloading Data using NSURLConnection in IOS using Objective-C

Downloading Data using NSURLConnection in IOS using Objective-C

There are several cases when you need to download an image from a specific url, so it is better that you know how to do it easily and effectively. Today I’m gonna show you how it can be achieved using NSURLConnection.

The case is that you downloaded and parsed your .json data and now you have a String of an url that you suppose to be displaying. How you do that? Well, surely you cannot set that string to be the image of your UIImageView, so you need to call that url and download it.

Next lets define a couple of fields that we’ll use to store data and keep track of our connection.

Now lets get to the fun part. Once you have a url for downloading a chunk of data, you can create your NSURLConnection object:

The first parameter is your NSURLRequest, followed by the object that implemented the NSURLConnectionDelegate that I mentioned earlier.

In this instance i’m going to tell it not to start immediately. The reason for this is that i don’t want other threads to block it from downloading. What i mean by this, is that if you were doing anything else while it was downloading, it could actually stop the connection from downloading until you finished.

Since we created the connection, but have not told it to start yet, we need to manually tell it to begin downloading.

By scheduling it in the main loop, we make sure that it doesn’t get blocked.

So now that we’ve begun downloading, lets capture the data that is returned. That will happen in the following protocol method:

Depending on how large your file is, this method can get called multiple times until your file is fully downloaded. If you know the size of the file you’re downloading, this is a good place to insert a progress indicator code that you can show your users.

If we haven’t instantiated our NSMutableData object yet, we’ll go ahead and do that here, and then append any data we’ve downloaded.

During the download there could be an error thrown, for example if the user lost their internet connection. That should be handled in the following protocol method:

Finally, when the download has completed the last method in our protocol will be invoked. At that point we can take our data and process it. For example, if it was JSON data, we could then use the NSJSONSerialization to load it:

One last thing. If for some reason something happens in your App and you want to stop downloading a particular connection, you can simply call cancel on your connection object:

[_connection cancel];

Downloading Data using NSURLConnection in IOS using Objective-C

Alternate Code