2

I had read that I can mark folders with "do not backup" attribute with ios 5.1 and later

As i understand, in such case all contents of directory will be excluded from backups.

In my app we need to exclude from backup all files in Documents directory (the files can be added or deleted from Documents during app execution).I need to store our files in Documents directory.

Can I mark Documents directory with "do not backup attribute"?

Does Apple permits this?

Could this become the reason to reject our app?

Purva
  • 1,291
  • 2
  • 15
  • 30
  • If you need it for Swift (iOS8.0) - just look at my answer here http://stackoverflow.com/a/24605684/1415713 – kurtanamo Jul 07 '14 at 08:07

4 Answers4

5

Yes you can set the do not backup flag for the folders(file) of Document directory.

do not backup attribute works on marked files regardless of what directory they are in, including the Documents directory.These files will not be purged and will not be included in the user's iCloud backup. Because these files do use on-device storage space, your app is responsible for monitoring and purging these files periodically.

for more information for the same go through this link

Below methods set the Do not back up flag to avoiding the unwanted backup|deletion from the app directory (Document and Cache).just need to call below method and pass url for the Folder(File).

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL {
    if (&NSURLIsExcludedFromBackupKey == nil) { // for iOS <= 5.0.1
        const char* filePath = [[URL path] fileSystemRepresentation];

        const char* attrName = "com.apple.MobileBackup";
        u_int8_t attrValue = 1;

        int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
        return result == 0;
    } else { // For iOS >= 5.1
        NSError *error = nil;
        [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
        return error == nil;
    }
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220
Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
  • Your quote on the Document directory says to me that you can make files IN the Documents directory to not be backed up, but not that the whole Documents folder itself can be. My interpretation anyway. – David H May 02 '13 at 12:40
  • @DavidH yes u right, what i think you should create a separate folder inside the Document directory for making it's whole content `do not backup`. – Kamar Shad May 02 '13 at 12:49
2

First, post this on the internal Apple forums and see if you can get an Apple engineer to respond, which I doubt. Problem is even if it works today it may break later.

Suggestions:

1) create a folder in Documents, mark it, then only store files in it.

2) add a method to your app delegate that takes a file name parameter, then when called locates the file and marks it. In this case you need to insure you call it after every file creation action.

David H
  • 40,852
  • 12
  • 92
  • 138
1

From the Apple docs, for iOS 5.1 or later, you should use this:

- (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;
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220
1
    func addSkipBackupAttributeToItemAtURL(filePath:String) -> Bool

{
    let URL:NSURL = NSURL.fileURLWithPath(filePath)
    assert(NSFileManager.defaultManager().fileExistsAtPath(filePath), "File \(filePath) does not exist")
    var success: Bool
    do {
        try URL.setResourceValue(true, forKey:NSURLIsExcludedFromBackupKey)
        success = true
    } catch let error as NSError {
        success = false
        print("Error excluding \(URL.lastPathComponent) from backup \(error)");
    }
    return success
}
Atef
  • 2,872
  • 1
  • 36
  • 32