Prevent application from backing up document folder?

I am trying to prevent my application from archiving files in iCloud, but it is completely confused and a little lost.

-Edit -

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

I want to prevent the backup of files that are uploaded to the applicationโ€™s document 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; } 

Then I call it with this code when the application starts:

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

The fingerprints prevent backup method called without error , but the application still shows that they have the same amount of data to back up with it earlier.

Any idea where this is going wrong?

-EDIT 2 -

Ok, I think this is resolved. Files are uploaded to a subfolder called "downloads". changing the code above to read it like this seems to accomplish the trick:

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

Thank you all for your help.

+4
source share
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 " this function

 [self addSkipBackupAttributeToItemAtURL:documentURLWithExtension]; 
+6
source

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 } 
+6
source

All Articles