I would like to download files directly from an URL to the disk using objective-c on the iPhone os.
Currently I am using NSURLConnection to send a synchronousRequest, writing the returned NSData into a file.
How can I change the download handling (still having the request beeing synchronous, it is already in a background thread) to write the data directly to disk, not using memory variables to store the complete content (only small parts)?
A sample code would be appreciated.
Thank you all in advance for your responses!
You can do this, but it’s a bit complicated to set up. Here’s how I’d do it:
warning: the following code was typed in a browser and compiled in my head. Also, there’s not a lot of error handling. Caveat Implementor.
The basic idea is that you create a standard
NSURLConnection, which is normally asynchronous, but just block the thread by spinning the runloop yourself until the connection is done. You also use a custom url connection delegate to just pipe any data the connection receives directly to a file.You can now do: