gpt4 book ai didi

ios - 无法在 Iphone 中创建文件夹,但可以在模拟器中使用

转载 作者:行者123 更新时间:2023-11-29 01:39:43 46 4
gpt4 key购买 nike

我的代码中有这样的东西

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);

最佳答案

我相信由于 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);
}

关于ios - 无法在 Iphone 中创建文件夹,但可以在模拟器中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32545772/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com