NSKeyedArchiver archiveRootObject和NSFileProtectionComplete / NSFileProtectionCompleteUnlessOpen

时间:2012-11-20 16:00:48

标签: ios nskeyedarchiver

实际上是否可以调用NSKeyedArchiver archiveRootObject并使用数据保护API将文件属性设置为NSFileProtectionComplete或NSFileProtectionCompleteUnlessOpen?

1 个答案:

答案 0 :(得分:6)

首先尝试将对象写入特定位置,在本例中为docFile(这是设置为文档目录)。然后将文件属性应用于docFile。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString* docFile = [docDir stringByAppendingPathComponent: @"Somefile.plist"];
NSError* error;    

 if([NSKeyedArchiver archiveRootObject:tempData toFile:docFile]) {

    NSDictionary *fileAttributes = [NSDictionary
                                dictionaryWithObject:NSFileProtectionComplete
                                forKey:NSFileProtectionKey];
    if([[NSFileManager defaultManager] setAttributes:fileAttributes     
                            ofItemAtPath:docFile  error: &error]) {
        NSLog(@"Success");
    }
 else {
   NSLog@(%@", error);
  }
}