iVela
iVela

Reputation: 1201

How to know if a file would be backed in iCloud?

My app was rejected because I use Documents directory to store a few files that I download from my server.

I've readed this from Apple's docs: https://developer.apple.com/library/ios/#qa/qa1719/_index.html and I've found this code: addSkipBackupAttributeToItemAtURL -> NSString parameter?

I was reading also that this code will be usefull for iOS versions >= 5.0.1. And that iOS versions < 5 don't use iCloud to backup user data. But that iOS == 5.0 uses iCloud to backup data but the flag dosn't work.

So, I have two questions:

First I need files in my application to work (like the database). And if I store those files in Caches directory whenever that iOS wants can delete my files. So I am storing my files in Documents directory ie:

/Documents/db.db
/Documents/my.plist

After that I store my files in that directory I call to the method addSkipBackupAttributeToItemAtURL for every file that I put in that directory. But I don't know how can I test if this is method is working.

I've readed here iOS 5.0.1 : How to verify that the folder is marked as "Do not back up" for iCloud? that if I use the simulator I can go to the Application's directory and run this command

xattr -plxv com.apple.MobileBackup <file name> 

to know if the file has been marked to not be backed up.

But if I run that command in the command line I get for every file this result:

xattr: my.plist: No such xattr: com.apple.MobileBackup

So, the first question is: how can I be sure that my files under Documents directory are being correctly marked to not be backed up. I don't want to upload again my app to the AppStore and that Apple's people throw it down again.

Second, my application runs with iOS 5. And I don't want to check for every file if the user has iOS 5.0 and store it in Caches directory and every time that I need to use the file check if the "stills" there. I have a 500mb database that I can't be downloading everytime.

So, second question is: if in "iOS Deployment Target" I choose 5.1 and in Build Settings > Base SDK I also choose 5.1 it means that users with iOS version < 5.1 wouldn't can download my app, right? So I don't need to be affraid of take of my data with users with iOS 5.0, right?

Upvotes: 4

Views: 2556

Answers (2)

Pawan Sharma
Pawan Sharma

Reputation: 3344

If your target is >= iOS 5.1 below code will help you. Use the below method for excluding the item from being backed up on iCloud. You can even use this code for excluding an entire folder

   - (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;
    }

Upvotes: 1

Sergio Moura
Sergio Moura

Reputation: 4942

I believe the answer is quite simple: Don't use the documents directory.

If you don't use the documents directory and do not mess with iCloud configuration, that data will not be backed up.

I believe you could use the library folder as described on the answer to this question: How can I get a writable path on the iPhone?

Edit: to answer the comment, I was wrong to assume that only Documents data were backed up, according to apple documentation at http://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html but I still believe that the Library folder is the best place for the non-backed-up database.

But, you should set a not-backup flag as explained in the same document, using the setResourceValue:forKey:error: method.

It is not clear on the documentation how to NOT backup data on iOS version == 5.0, unless you're willing to recreate deleted cache data (which I believe you don't, hence your question).

If is not an issue, lock your app to work only on iOS 5.1 or above (set your app to work on a minimum iOS version on XCode, remember to clean your whole build and build it again).

Upvotes: 2

Related Questions