2

I have a file which I created using NSFileManager but could not find a way to make it readonly. I have searched all over the internet and in Apple's NSFileManager Class Reference but could not find anything. Here is the code of where the file was created.

if ([fm createFileAtPath:fileName contents:inputData attributes:nil] == NO) {
     NSLog(@"Error!");
     return 1;
}
Maanit
  • 316
  • 2
  • 4
  • 14

2 Answers2

4

Use setAttributes: of [NSFileManager defaultManager] to remove the files read bit. This posts explains it.

Following sets permissions to -rwxrwxrwx (0777). Replace 0777 with the permissions you desire.

NSDictionary *attributes;
[attributes setValue:[NSNumber numberWithShort:0777] forKey:NSFilePosixPermissions];
[[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:@"/path/to/file"]
Community
  • 1
  • 1
Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72
  • Thank you so much. I am new to this so can you provide an example of using the `setAttributes` preferably with this example – Maanit Apr 06 '15 at 17:28
1

Use :

[attributes setValue:[NSNumber numberWithShort:0777] forKey:NSFilePosixPermissions];

[filemanager setAttributes:atributes ofItemAtPath:yourPath error:nil];
itsji10dra
  • 4,603
  • 3
  • 39
  • 59