Robert Robert - 4 months ago 34
Objective-C Question

Prevent app from backing up documents folder?

I'm trying to prevent my app backing up files to iCloud but have become completely confused and a little lost.

-EDIT-

I've updated this to reflect the changes I've made thanks to the posters below.

I want to prevent back up of files which are downloaded to the app's documents directory.

So far I have a class called

PreventBackup
with the following method:

+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
}
NSLog(@"prevent backup method called without error");
return success;
}


I'm then calling it with this code when the app starts:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *pathURL= [NSURL fileURLWithPath:documentsDirectory];
[PreventBackup addSkipBackupAttributeToItemAtURL:pathURL];


The cosole prints
prevent backup method called without error
but the app is still showing as having the same amount of data for back up with it did before.

Any idea where this is going wrong?

-EDIT 2-

OK, I think this is solved. The files are downloading to a sub folder called "downloads". changing the code above so that it reads as follows appears to have done the trick:

NSString *downloadsFolder = [documentsDirectory stringByAppendingPathComponent:(@"/downloads")];
NSURL *pathURL= [NSURL fileURLWithPath:downloadsFolder];
[PreventBackup addSkipBackupAttributeToItemAtURL:pathURL];


Thanks to all for your help.

Answer
- (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;
}

NSURL *documentURLWithExtension = [documentURL URLByAppendingPathExtension:extensionType];

pass this "documentURLWithExtension" to this function

[self addSkipBackupAttributeToItemAtURL:documentURLWithExtension];