Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 545
Robert Avatar asked Dec 15 '22 16:12

Robert


2 Answers

- (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];
like image 164
aahsanali Avatar answered Dec 18 '22 05:12

aahsanali


In Swift:

//Path of document directory
var docPathAry : NSArray! = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var docDirPathStr: AnyObject? = docPathAry.count > 0 ? docPathAry[0] : nil

self.addSkipBackupAttributeToItemAtURL(NSURL.fileURLWithPath(docDirPathStr as NSString))

and:

func addSkipBackupAttributeToItemAtURL(URL: NSURL!) -> Bool {
    assert(NSFileManager.defaultManager().fileExistsAtPath(URL.path))
    var err : NSError? = nil
    var success : Bool! = URL.setResourceValue(NSNumber.numberWithBool(true), forKey: NSURLIsExcludedFromBackupKey, error: &err)

    if(!success) {
        println("Error excluding \(URL.lastPathComponent) from backup\(err) ")
    }

    return success
}
like image 20
Renish Dadhaniya Avatar answered Dec 18 '22 07:12

Renish Dadhaniya