I have a folder on my remote server that has a few .png files in it. I want to download these from within my app and store them in the apps ‘Documents’ folder. How can I do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The easy way is to use NSData’s convenience methods
initWithContentOfURL:andwriteToFile:atomically:to get the data and write it out, respectively. Keep in mind this is synchronous and will block whatever thread you execute it on until the fetch and write are complete.For example:
Where the
documentsDirectorymethod is stolen shamelessly from this question:However, unless you intend to thread it yourself this will stop UI activity while the file downloads. You may instead want to look into NSURLConnection and its delegate – it downloads in the background and notifies a delegate about data downloaded asynchronously, so you can build up an instance of NSMutableData then just write it out when the connection’s done. Your delegate might contain methods like:
The little details, like declaring the
dataAccumulatorand handling errors, are left to the reader 🙂The important documents: