The problem is that dataWithContentsOfURL:
is a blocking call. This means that it will block the thread that it is running on.
You've got a couple of options to fix this, and the better one is probably to use an NSURLConnection
.
With an NSURLConnection
you can perform the download request asynchronously, which will prevent it from blocking the main thread.
You must use the NSURLConnectionDelegate
methods to be informed of the progress of the download, save its data, and to be informed of success or failure.
Please read the documentation for the NSURL Loading System.
An alternative to using NSURLConnection
is to wrap your current code with some calls to GCD using dispatch queues. This will prevent the call from blocking your UI, but it will not allow you to determine the progress - for that you will still need to use NSURLConnection
.