Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download files from internet and save in 'Documents' on iPhone?

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?

like image 249
RexOnRoids Avatar asked Aug 26 '09 02:08

RexOnRoids


1 Answers

The easy way is to use NSData's convenience methods initWithContentOfURL: and writeToFile: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:

// Create and escape the URL for the fetch
NSString *URLString = @"http://example.com/example.png";
NSURL *URL = [NSURL URLWithString:
              [URLString stringByAddingPercentEscapesUsingEncoding:
                          NSASCIIStringEncoding]];

// Do the fetch - blocks!
NSData *imageData = [NSData dataWithContentsOfURL:URL];
if(imageData == nil) {
    // Error - handle appropriately
}

// Do the write
NSString *filePath = [[self documentsDirectory] 
                      stringByAppendingPathComponent:@"image.png"];
[imageData writeToFile:filePath atomically:YES];

Where the documentsDirectory method is stolen shamelessly from this question:

- (NSString *)documentsDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

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:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Append the data to some preexisting @property NSMutableData *dataAccumulator;
    [self.dataAccumulator appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // Do the write
    NSString *filePath = [[self documentsDirectory] 
                          stringByAppendingPathComponent:@"image.png"];
    [imageData writeToFile:filePath atomically:YES];
}

The little details, like declaring the dataAccumulator and handling errors, are left to the reader :)

The important documents:

  • NSData
  • NSURLConnection
like image 70
Tim Avatar answered Nov 15 '22 03:11

Tim