22

I want to set Do not back up to my folder which is in Document Directory.

I found code for "Do not back up" , but how do i verify that the folder is marked.

3
  • Did you get this working? I am also trying something similar for directory in Documents. Do we have to do this for all files under directory? Nov 23, 2011 at 9:27
  • @AppleDeveloper : Yes, you have to mark your directory if you dont want to back up on iCloud. I have tried this. but waiting for apple's reviews.
    – Devang
    Nov 24, 2011 at 4:15
  • Cool. Me too waiting for Apple's review. I have given my code at stackoverflow.com/questions/8209755/…. Please let me know if you see any issues. Thanks. Nov 24, 2011 at 9:18

5 Answers 5

21

For iOS 5.1, run the app in the simulator, and run the following command in the Terminal:

xattr {filename}

You should see the following if the item is correctly marked for exclusion:

com.apple.metadata:com_apple_backup_excludeItem 
2
  • 1
    if I apply the "Do not backup" property to a directory, the files on it won't be synced? Because I applied it to a folder (../Library/Folder/file) and when I use "xattr ../Library/Folder" the "com.apple.metadata:com_apple_backup_excludeItem" shows up, but when I use "xattr ../Library/Folder/file" it doesn't. Sep 3, 2012 at 10:29
  • Did not work without doing du -sk - see @user353877 answer
    – Rotem
    Sep 14, 2020 at 12:55
12

According to the docs you linked, if you set the method up exactly how they have it listed on that page, the method will return YES if the attribute is marked correctly.

5
  • ok. But from the document I found Important: The new "do not back up" attribute will only be used by iOS 5.0.1 or later. On iOS 5.0 and earlier, applications will need to store their data in <Application_Home>/Library/Caches to avoid having it backed up. Since this attribute is ignored on older systems, you will need to insure your app complies with the iOS Data Storage Guidelines on all versions of iOS that your application supports. So Do I need to change my folder location to library for Apple approval.
    – Devang
    Nov 14, 2011 at 4:54
  • 3
    Your app can check the OS version and do the appropriate thing.
    – hotpaw2
    Nov 14, 2011 at 14:32
  • Please correct me if I'm wrong. It seems like there are three cases: Pre 5.0, 5.0, and 5.0.1. For 5.0.1, you can mark your folder as "Offline Data". In 5.0, iCloud exists, but without a "do not back up" solution, so you'll have to put data in Library/Caches, where it will be purged :(. Pre 5.0 doesn't have iCloud, and AFAIK data wont be purged from Documents or Caches, so it's probably safe to put data in Caches here as well.
    – Michael
    Nov 28, 2011 at 18:37
  • 2
    @Michael My understanding is that for Pre 5.0 and for 5.0 you can put offline data (data that can be downloaded or otherwise recreated, but that the user expects to be reliably available when offline) in the <Application_Home>/Documents directory or in the <Application_Home>/Library/Private Documents directory. It will be backed up for 5.0 but that's ok. Dec 5, 2011 at 18:35
  • @NikolaiSander - have you verified that it is ok for Pre 5.0 and 5.0 for Apple approval? I have the 'do not back up' flag set for 5.0.1 but really don't want to come up with a solution just for 5.0.
    – FishStix
    Feb 23, 2012 at 2:32
12
  1. open simulator
  2. delete the app (the app review team runs the code 1 time through, so be sure you are starting from scratch)
  3. now run your code in simulator, your code will store your files on your harddrive.
  4. open up terminal
  5. cd /Users/(user name)/Library/Application Support/iPhone Simulator/(ios version)/Applications
  6. ls
  7. you will be presented with a list of random folder names, open each one until you find your app folder
  8. for file in $(find *); do du -sk $file; xattr -l $file; echo ; done
  9. you will be presented with all of the files in the app in the format of..

    (filesize) (filename)
    (iCloud backup exclusion)

NOTE: if nothing is listed, that means the file will be backed up (if its in the Library or Documents folder).. if instead you see 'com.apple.metadata:com_apple_backup_excludeItem: com.apple.MobileBackup' then you're good to go.

3
  • when i do this, i see 'com.apple.MobileBackup:' instead of 'com.apple.metadata:com_apple_backup_excludeItem: com.apple.MobileBackup'. Does this mean the flag has not been correctly set? I'm just using the method provided here to set the flag: developer.apple.com/library/ios/#qa/qa1719/_index.html
    – usrgnxc
    Jun 9, 2013 at 22:43
  • xattr always returns nothing for files from iOS Simulator.. Does it mean there is no 'extended attributes'?
    – GML-VS
    Sep 3, 2014 at 14:18
  • This worked for me, I had to do du -sk so I think this should be the accepted answer
    – Rotem
    Sep 14, 2020 at 12:54
10

Run the app in the simulator, then use the Terminal to run this command against the relevant files:

xattr -plxv com.apple.MobileBackup <file name>
2
  • Thank you , what is the <file name> ? is it my application name ? or what ?
    – iOS.Lover
    Oct 29, 2013 at 10:51
  • xattr always returns nothing for files from iOS Simulator.. Does it mean there is no 'extended attributes'?
    – GML-VS
    Sep 3, 2014 at 14:19
2

For iOS 5.1 or later this code works fine for me.

   - (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *)filePathString {
        NSURL *fileURL =
        [NSURL fileURLWithPath:filePathString];

        assert([[NSFileManager defaultManager] 
        fileExistsAtPath: [fileURL path]]);

        NSError *error = nil;

        BOOL success = [fileURL setResourceValue:[NSNumber numberWithBool:YES]
                                      forKey:NSURLIsExcludedFromBackupKey
                                     error:&error];
        return success;
    }
2
  • Proactive Programmer I put on appDelegate but Apple rejected again !!! it's strange I just load file with [NSbundle mainBundle]; I don't store anything ! would you please help me to make this through ?
    – iOS.Lover
    Oct 28, 2013 at 13:14
  • If you are using App's Document directory for temporary holding files from the App Bundle then you can use Cache directory in this case. If you want it permanently stored then you cane even apply do not back up attribute on the whole directory as well. Oct 28, 2013 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.