Objective-C:以编程方式为当前用户设置文件权限

mik*_*wan 0 macos cocoa nsfilemanager

我正在研究管理文档的程序.我的应用程序所做的一件事是从互联网下载文档并打开它们.我下载的这些文件需要以只读权限打开.

所以,我想知道是否有一种以编程方式设置文件权限的方法.

提前致谢.

hig*_*ted 5

除了chmod通过@Vlad提到的,你也可以使用setAttributes:ofItemAtPath:error:的方法NSFileManager用的属性NSFileImmutable:

NSDictionary* attribs = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSFileImmutable];
[[NSFileManager defaultManager]  setAttributes: attribs ofItemAtPath:@"/path/to/file" error:nil];
Run Code Online (Sandbox Code Playgroud)