Friday, August 30, 2013

Downloading Content to Local iOS Documents Directory

This example shows you how to download a remote file into an iOS Documents Directory from your iOS device. This is helpful for updates, and persisting data into your application, to replace static data. This allows you to keep your data fresh and dynamic.

- (void) downloadNewContent {
    NSLog(@"Downloading New Content...");
    NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/temp"];
    NSError *error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
    
    NSURL *url = [NSURL URLWithString:@"http://naep-sp2010dev.naepims.org:8889/update.zip"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    if(data) {
        stringPath = [stringPath stringByAppendingPathComponent:[url lastPathComponent]];
        [data writeToFile:stringPath atomically:YES];
    }
    

}

No comments:

Post a Comment