NSKeyedArchiver archiveRootObject and NSFileProtectionComplete/NSFileProtectionCompleteUnlessOpen

StackOverflow https://stackoverflow.com/questions/13477106

  •  30-11-2021
  •  | 
  •  

سؤال

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

هل كانت مفيدة؟

المحلول

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);
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top