Last active
December 31, 2015 19:39
-
-
Save pyanfield/8035235 to your computer and use it in GitHub Desktop.
[ios] get file from web and save on the disk
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
// src : file's url | |
// file : file's name on the disk | |
// return : file's path on the disk | |
- (NSString*)catchFileFrom:(NSString*)src toFile:(NSString*)file | |
{ | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],file]; | |
NSURL *url = [NSURL URLWithString:src]; | |
NSData *urlData = [NSData dataWithContentsOfURL:url]; | |
[urlData writeToFile:filePath atomically:YES]; | |
return filePath; | |
} | |
- (NSString*)catchFileFrom:(NSString*)src saveAtPath:(NSString*)path named:(NSString*)file | |
{ | |
NSLog(@" %@ >> %@",file,src); | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *docsDir = [paths objectAtIndex:0]; | |
NSString *filePath; | |
if(path == nil){ | |
filePath = [NSString stringWithFormat:@"%@/%@",docsDir,file]; | |
}else{ | |
NSFileManager *filemgr = [NSFileManager defaultManager]; | |
NSString *newDir = [docsDir stringByAppendingPathComponent:path]; | |
if ([filemgr createDirectoryAtPath:newDir withIntermediateDirectories:YES attributes:nil error: NULL]) { | |
filePath = [NSString stringWithFormat:@"%@/%@",newDir,file]; | |
}else{ | |
return nil; | |
} | |
} | |
NSURL *url = [NSURL URLWithString:src]; | |
NSData *urlData = [NSData dataWithContentsOfURL:url]; | |
[urlData writeToFile:filePath atomically:YES]; | |
return filePath; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment