Search code examples
iosobjective-cfile-permissionsnsfilemanager

Create file with specific permisions in iOS


In my project i'm creating a file using the function:

- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;

I want to grant this file permissions so only the user can read and write (-rw------). I cant figure out (or find documentation) how i should fill this attribute dictionary (keys and values).

Thanks in advance


Solution

  • It's not clear what you expect to gain from this, but the syntax is:

    NSDictionary *attributes = @{
        NSFilePosixPermissions : @((short)(0x600))
    };
    BOOL created = [[NSFileManager defaultManager] createFileAtPath:path
                                                           contents:data
                                                         attributes:attributes];
    

    Not sure if you actually need the (short) cast or not.