0

My app needs to download a decent amount of contents from a server to be able to be functional.

Apple guidelines about data storage mentions that this kind of data, "needed-to-work-but-easily-refetchable" shouldn't be included in iCloud/iTunes backups: fair enough.

The tricky part is that the code to prevent a directory to be backed up is different between iOS 5.0, 5.0.1 and 5.1 (cf. this technical note).

My app currently supports iOS 5.0 as a deployment target.

What should I do between the different following options:

  • set the deployment target to 5.1 (straight-forward but i can't find data about proportion of users still being in iOS 5.0 and 5.0.1 to be comfortable introducing the subject to my boss)
  • implementing both 5.0.1 and 5.1 codes provided by Apple but it raises some issues:
    • my usual way to detect if a device is running a speficic iOS version is to use respondsToSelector: with a selector introduced in the iOS version i'm targeting but iOS 5.1 seems to introduce constants and not-universal classes only. How to be sure i'm running iOS 5.1 or later?
    • what about devices running iOS 5.0? storing data into Caches would be super annoying to deal with both for the development team and the user experience

Any other option to recommend?

Mick F
  • 7,312
  • 6
  • 51
  • 98
  • I previously provided the exact solution to this question here: http://stackoverflow.com/q/12371321/1633251 - if you like the Q&A please upvote both. – David H Oct 19 '12 at 11:39

3 Answers3

0

For each file that is being stored to the file system, you have to add "Do not backup" attribute. See this answer: Adding the "Do Not Backup" attribute to a folder hierarchy in iOS 5.0.1. To check the system version you can call [[UIDevice currentDevice] systemVersion], or use macros like __IPHONE_5_0 with #if defined() and #ifndef. Good luck!

Community
  • 1
  • 1
Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
  • That's what i was doing before but now that i'm running on iOS 6 devices, the setxattr method doesn't return 0 anymore. So this method is not enough anymore. – Mick F Oct 19 '12 at 09:39
  • Thanks for the __IPHONE_5_0 tip! (as of the systemVersion, i reckon it is the most straightforward way to get the iOS version but i don't like it as it requires some NSString parsing and some expectation of what the yet-to-come iOS versions names will be) – Mick F Oct 19 '12 at 09:41
0

I'm marking my images folder not to be backed up as I'm downloading images for offline usage. Here apple discuss how to do so.

#include<sys/xattr.h>

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL {
    if (&NSURLIsExcludedFromBackupKey == nil) { // 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 { // iOS >= 5.1
        NSError *error = nil;
        [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
        return error == nil;
    }
}

- (void)createSkipBackupImagesFolder {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/images"];

    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
        NSURL *toURL = [NSURL fileURLWithPath:dataPath];
        [self addSkipBackupAttributeToItemAtURL:toURL];
    }
}

[self createSkipBackupImagesFolder];
zeeawan
  • 6,667
  • 2
  • 50
  • 56
-1

if you store your data into Cache directory instead of document directory than icloud will not backup your data...

so i think this is the best way to stop icloud backup.

in "do not backup" attribute you need to set this flag for each and every file.

use NSCachesDirectory insted NSDocumentDirectory.

Sarafaraz Babi
  • 480
  • 6
  • 18
  • Files can get deleted from cache directory at any time and, "don't backup" attribute can be set to a folder, which means, files in that folder also will not be backed up. If you are going to help people, be sure that your help will help them at least for 70% like apple :) – Fahri Azimov Oct 19 '12 at 09:39
  • Hello fahri thanks for down vote...but my one application is rejected by apple for this icloud backup problem. after that i have searched lot and found solution for cache directory... – Sarafaraz Babi Oct 19 '12 at 09:44
  • and then after i have uploaded 3 application on store and it's works perfectly with the cache directory...no data will be deleted from this directory... – Sarafaraz Babi Oct 19 '12 at 09:46
  • i think you need to checkout this link...http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html – Sarafaraz Babi Oct 19 '12 at 09:47
  • @DirtyHenry i just wants to helps you. if you like my solution then go with this else not...But my solution is perfect.my company is using this for all the application...Best luck dear. – Sarafaraz Babi Oct 19 '12 at 09:51
  • @Sarafaraz: maybe in real life, the contents of the Caches directory are never deleted. But as the official documentation says so, I'd rather not pick this option. – Mick F Oct 19 '12 at 09:51
  • Yep, I'm aware of those problems, and I have seen and experienced cache cleanup on iOS. When iOS device runs low on hard memory, it starts cleaning up all cache directories of apps. So, I think you have to read more, dear :) – Fahri Azimov Oct 19 '12 at 10:05