4

经过半天的挖掘,我发现我没有创建文件夹的权限,那么我如何获得创建文件夹的权限?

[[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"}
4

2 回答 2

7

您无法修改已编译应用程序包文件夹的内容。这是因为捆绑包是已编译的应用程序。这可以防止恶意软件在安装后修改应用程序的可能性。

运行时生成的文件应保存到或文件夹中Documents。这些文件夹只能由您的应用访问。没有其他应用程序可以访问这些文件夹的内容。TempCache

示例代码:

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];
于 2013-09-02T13:04:19.797 回答
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)
于 2015-01-29T04:27:37.097 回答