Created
March 24, 2015 18:05
-
-
Save mako34/1217a4d1811a6b31c2ef to your computer and use it in GitHub Desktop.
ios Save file to my file sys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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