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

早过忘川 提交于 2019-12-21 02:26:12

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/1332127/how-to-download-files-from-internet-and-save-in-documents-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!