2015-01-30 4 views

답변

4

파일을 만드는 방법에 따라 파일 특성을 지정할 수 있습니다. 파일을 읽기 전용으로 만들려면, 다음과 같은 속성을 전달합니다

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

참고 값에서 최고의 0합니다. 중요합니다. 이것은 이것이 8 진수임을 나타냅니다.

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

당신을 수행 기존의 권한을 보장하기 위해

이 유지됩니다, 다음을 수행 그것을 만든 후

또 다른 옵션은 파일의 속성을 설정하는 것입니다 기존 속성을 가져오고 새 속성을 병합 할 필요가 없습니까? –

+1

@David 파일에 다른 특정 속성이 있으면 yes로 설정하는 것이 좋습니다. – rmaddy

+0

감사합니다 얘들 아, 시도하고 내 발견을 알려 드리겠습니다. – Bhat

관련 문제