我的代码中有这样的东西
if(![[NSFileManager defaultManager] createDirectoryAtPath:uploadDirPath withIntermediateDirectories:true attributes:nil error:nil]) {
HTTPLogError(@"Could not create directory at path: %@", uploadDirPath);
}
uploadDirPath
的内容是:
/Users/UserJ/Library/Developer/CoreSimulator/Devices/535C67AB-F932-417B-8CC3-9A813F4F402C/data/Containers/Data/Application/347FA1B2-F790-46A6-B0D6-B638CF2F43F0/Music/
上面的代码在模拟器中工作正常,但是当我在手机中运行它时会遇到无法创建文件夹的情况。
字符串显示在手机上时的内容是
/var/mobile/Containers/Data/Application/3E115818-0619-46B0-BD13-BD35EFDF50E3/Music/
有关为何无法在手机上创建文件夹的任何建议?
更新
这是之前的代码
NSArray* dirPaths = NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES);
NSString* MusicDir = [dirPaths objectAtIndex:0];
//------
MultipartMessageHeaderField* disposition = [header.fields objectForKey:@"Content-Disposition"];
NSString* filename = [[disposition.params objectForKey:@"filename"] lastPathComponent];
//--------
if ( (nil == filename) || [filename isEqualToString: @""] ) {
// it's either not a file part, or
// an empty form sent. we won't handle it.
return;
}
//NSString* uploadDirPath = [[config documentRoot] stringByAppendingPathComponent:@"upload"];
NSString* uploadDirPath = [MusicDir stringByAppendingString:@"/"];
BOOL isDir = YES;
if (![[NSFileManager defaultManager]fileExistsAtPath:uploadDirPath isDirectory:&isDir ]) {
[[NSFileManager defaultManager]createDirectoryAtPath:uploadDirPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString* filePath = [uploadDirPath stringByAppendingPathComponent: filename];
if( [[NSFileManager defaultManager] fileExistsAtPath:filePath] )
{
//Delete this file if it exists
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
我注意到在Iphone上运行的数组是空的,但是当它在模拟器上运行时很好
NSArray* dirPaths = NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES);
答案 0 :(得分:1)
我相信由于iOS设备中的应用程序以沙盒方式运行,因此AppData文件夹结构是预定义的。请尝试在Documents目录中创建Music目录,它应该可以工作:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirPath = paths.firstObject;
NSString *uploadDirPath = [documentsDirPath stringByAppendingPathComponent:@"Music"];
if (![[NSFileManager defaultManager] createDirectoryAtPath:uploadDirPath withIntermediateDirectories:true attributes:nil error:nil]) {
HTTPLogError(@"Could not create directory at path: %@", uploadDirPath);
}