如何将权限设置为iOS中的文件夹和文件,该文件夹位于documents文件夹中?
在“文档”文件夹中创建文件时,是否可以设置只读权限?
或者其他的解决方案?
发布于 2015-01-30 16:18:08
根据创建文件的方式,可以指定文件属性。若要使文件只读,请传递以下属性:
NSDictionary *attributes = @{ NSFilePosixPermissions : @(0444) };注意值中的前导0。这一点很重要。它表示这是一个八进制数。
另一个选项是在文件创建后设置其属性:
NSString *path = ... // the path to the file
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
if (![fm setAttributes:attributes ofItemAtPath:path error:&error]) {
NSLog(@"Unable to make %@ read-only: %@", path, error);
}更新:
要确保保留现有权限,请执行以下操作:
NSString *path = ... // the path to the file
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
// Get the current permissions
NSDictionary *currentPerms = [fm attributesOfFileSystemForPath:path error:&error];
if (currentPerms) {
// Update the permissions with the new permission
NSMutableDictionary *attributes = [currentPerms mutableCopy];
attributes[NSFilePosixPermissions] = @(0444);
if (![fm setAttributes:attributes ofItemAtPath:path error:&error]) {
NSLog(@"Unable to make %@ read-only: %@", path, error);
}
} else {
NSLog(@"Unable to read permissions for %@: %@", path, error);
}https://stackoverflow.com/questions/28239577
复制相似问题