简体   繁体   English

如何在iOS中设置文件夹/文件的权限

[英]How to Set Permission for folders/files in iOS

How can I set permission to folders and files in iOS which is there inside documents folder? 如何设置iOS内部文档文件夹中的文件夹和文件的权限?

Is it possible to set read only permission while creating files inside documents folder? 在文档文件夹中创建文件时是否可以设置只读权限?

Or any alternative solution ? 或任何替代解决方案?

Depending on how you create the file, you can specify file attributes. 根据您创建文件的方式,您可以指定文件属性。 To make a file read-only, pass the following attributes: 要使文件成为只读,请传递以下属性:

NSDictionary *attributes = @{ NSFilePosixPermissions : @(0444) };

Note the leading 0 in the value. 注意值中的前导0 That's important. 这很重要。 It indicates that this is an octal number. 它表示这是一个八进制数。

Another option is to set the file's attributes after it has been created: 另一个选项是在创建文件后设置文件的属性:

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

Update: 更新:

To ensure existing permissions are kept, do the following: 要确保保留现有权限,请执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 iOS 上启用文件和文件夹权限 - How to enable files and folders permission on iOS 如何为IOS应用设置Google Drive ReadOnly文件权限 - How to set Google Drive ReadOnly files Permission for IOS app 如何在ios中设置FBLoginview的用户读取权限? - How to set User read permission on FBLoginview in ios? iOS / Swift 4:如何在不删除文件和文件夹现有目标的情况下复制包含多个包含文件的文件夹的文件夹? - iOS/Swift 4: How to copy a folder containing several folders containing files without removing the files and folders existing destination? 如何在iOS中以编程方式从文档文件夹重命名文件/文件夹 - How to rename files/folders from document folder programmatically in iOS 如何为文件夹中的所有文件和文件夹设置NSURL资源值 - How to set NSURL Resource Values for all files and folders within a folder iOS Facebook SDK v4.1-如何根据需要设置权限 - iOS Facebook SDK v4.1 - How to set permission as required CloudKit iOS我该如何设置World Write Permission - CloudKit iOS how could I set World Write Permission iOS-Access App创建的文件和文件夹 - iOS - Access App created Files and Folders AWS IOS SDK v2.0:如何获取存储桶中的所有文件夹,然后从所述文件夹中提取所有文件? - AWS IOS SDK v2.0: How do I get all the folders within a bucket, and then pull all the files from said folders?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM