我想要在文件夾「tracks」中的documentsDirectory中下載一個文件。但是如果我使用這個代碼(在文檔目錄中下載文件)。所有做工精細如何從文件夾下載文件?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Chapter.mp3"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:false];
NSString *stringURL = @"link";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
如果我(在文件夾「軌道」文件下載在documentsDirectory)使用此代碼文件沒有下載
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"tracks/Chapter.mp3"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:false];
NSString *stringURL = @"link";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
如何在文件夾中下載文件documentsDirectory「軌道」 ?
嘗試添加;如果(![fileManager fileExistsAtPath:filePath isDirectory:&isDir]) 如果(![fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:NULL]) NSLog(@「Error:Create folder failed%@」,directory); –