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.

Devang
  • 11,258
  • 13
  • 62
  • 100
  • 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? – Paresh Masani Nov 23 '11 at 09: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 '11 at 04:15
  • Cool. Me too waiting for Apple's review. I have given my code at http://stackoverflow.com/questions/8209755/iphone-move-resources-to-application-bundle. Please let me know if you see any issues. Thanks. – Paresh Masani Nov 24 '11 at 09:18

5 Answers5

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 
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Aditya Rathnam
  • 219
  • 2
  • 3
  • 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. – tomidelucca Sep 03 '12 at 10:29
  • Did not work without doing `du -sk` - see @user353877 answer – Rotem Sep 14 '20 at 12:55
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.

user353877
  • 1,211
  • 1
  • 15
  • 21
  • 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: https://developer.apple.com/library/ios/#qa/qa1719/_index.html – usrgnxc Jun 09 '13 at 22:43
  • xattr always returns nothing for files from iOS Simulator.. Does it mean there is no 'extended attributes'? – GML-VS Sep 03 '14 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 '20 at 12:54
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.

esqew
  • 42,425
  • 27
  • 92
  • 132
  • 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 /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 '11 at 04:54
  • 3
    Your app can check the OS version and do the appropriate thing. – hotpaw2 Nov 14 '11 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 '11 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 /Documents directory or in the /Library/Private Documents directory. It will be backed up for 5.0 but that's ok. – Nikolai Sander Dec 05 '11 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 '12 at 02:32
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>
noodl_es
  • 1,467
  • 1
  • 17
  • 28
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;
    }
Pawan Sharma
  • 3,199
  • 1
  • 25
  • 32
  • 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 '13 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. – Pawan Sharma Oct 28 '13 at 13:47