经过一半的挖掘,我发现我没有创建文件夹的权限,所以我如何获得创建文件夹的权限?
[[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@",downloadLocation,kAmazonCatalogsPrefix]
withIntermediateDirectories:YES
attributes:nil
error:&error];
Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1f18a8b0 {NSFilePath=/var/mobile/Applications/AB570058-4E4A-41B2-9E40-5C93316D6307/eCatalogs, NSUnderlyingError=0x1f18a830 "The operation couldn’t be completed. Operation not permitted"}
答案 0 :(得分:7)
您无法修改已编译应用的捆绑文件夹的内容。这是因为bundle是已编译的应用程序。这可以防止安装后恶意软件修改应用程序的可能性。
运行时生成的文件应保存到Documents
,Temp
或Cache
文件夹中。这些文件夹只能由您的应用访问。没有其他应用可以访问这些文件夹的内容。
示例代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:[documentsDirectory stringByAppendingPathComponent:@"yourFolderName"] withIntermediateDirectories:NO attributes:nil error:&error];
答案 1 :(得分:7)
检查下面的SWIFT答案:
var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
[NSFileManager.defaultManager().createDirectoryAtPath(paths.stringByAppendingPathComponent("Hello"), withIntermediateDirectories: false, attributes: nil, error: nil)]
println(paths)