NSKeyedArchiver archiveRootObject and NSFileProtectionComplete/NSFileProtectionCompleteUnlessOpen

ios, nskeyedarchiver

Solution

First attempt to write the object to the specific location, in this case docFile (this is set to the documents directory). Then apply the file attributes to the 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);
  }
}

Problem

Is it actually possible to call NSKeyedArchiver archiveRootObject and work with data protection API setting the file attribute to NSFileProtectionComplete or NSFileProtectionCompleteUnlessOpen ?

Original source