Skip to content

Instantly share code, notes, and snippets.

@mako34
Created March 24, 2015 18:05
Show Gist options
  • Select an option

  • Save mako34/1217a4d1811a6b31c2ef to your computer and use it in GitHub Desktop.

Select an option

Save mako34/1217a4d1811a6b31c2ef to your computer and use it in GitHub Desktop.
ios Save file to my file sys
//download the file in a seperate thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Downloading Started");
NSString *urlToDownload = @"http://www.somewhere.com/thefile.png";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved !");
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment