I have a huge tree of files and dirs for cache in my document directory.
As recommended, I'm going to use NSURLIsExcludedFromBackupKey
for preventing iTunes from saving this data with the app.
Can I use it once on my root directory URL, as
[rootDirectoryURL setResourceValue:[NSNumber numberWithBool:YES] forKey:@"NSURLIsExcludedFromBackupKey" error:&error];
Or will I have to call it for each file?
Yup, you can pass it a NSURL of the directory you want excluded.
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue:[NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
}
return success;
}
And you can test any files if in doubt using
id flag = nil;
[URL getResourceValue: &flag
forKey: NSURLIsExcludedFromBackupKey error: &error];
NSLog (@"NSURLIsExcludedFromBackupKey flag value is %@", flag)
From my tests (on a simulator), it is not recursive, and I've just had an app rejection because of too much data in Documents directory. Each folder directly in the Documents directory is flagged, so I guess the files inside them are not, even on a real device. But I also added new content in the folder, so I may just need to add the flag again.
So I'm using this recursive method now, and I add the flag on each new files added :
- (void) addSkipBackupAttributeToFolder:(NSURL*)folder
{
[self addSkipBackupAttributeToItemAtURL:folder];
NSError* error = nil;
NSArray* folderContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[folder path] error:&error];
for (NSString* item in folderContent)
{
NSString* path = [folder.path stringByAppendingPathComponent:item];
[self addSkipBackupAttributeToFolder:[NSURL fileURLWithPath:path]];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With