Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

iphone - Where should I save data & files I want to keep long term, and how do I prevent iCloud from backing them up

I have file - of arbitrary type including Core Data repositories - that I need to keep around and not have iOS delete them. Usually, I do not want iCloud to back these files up. Where should I save them?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Saving files locally changed from iOS5.0 and earlier, 5.0.1, and 5.1 and newer primarily to the address iCloud backup issues. There are two Apple source documents (the File System Programming Guide, and QA1719) that together provide the information supporting the following:

  • iOS 5.0

Files should be saved in the "Caches" directory, as there is no way to prevent backups if they are stored in the Documents folder. Note that the system may remove these files (see QA1719), so you would need the ability to recreate each as needed. To find the caches directory, use this:

[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
  • iOS 5.0.1

Files should be saved in '/Library/Application Support' (FSP, page 15), which can be better accessed via:

[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject]

My experience is that this directory doesn't always exist, and thus you may need to create it:

- (NSString *)applicationAppSupportDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject];
}

NSFileManager *manager = [NSFileManager defaultManager];
NSString *appSupportDir = [self applicationAppSupportDirectory];
if(![manager fileExistsAtPath:appSupportDir]) {
    __autoreleasing NSError *error;
    BOOL ret = [manager createDirectoryAtPath:appSupportDir withIntermediateDirectories:NO attributes:nil error:&error];
    if(!ret) {
        NSLog(@"ERROR app support: %@", error);
        exit(0);
    }
}

Files saved to this directory (or subdirectories) need an extended attribute to tell iCloud not to back them up (see QA1719).

PS: I have not found a way to set the Deployment target to this release, if there is a way please leave a comment.

  • iOS 5.1

Files (or folders of files) should be located in the 'Application Support' folder as described above. To prevent iCloud from backing up use:

[URL setResourceValue: [NSNumber numberWithBool: YES] ????forKey:NSURLIsExcludedFromBackupKey error:&error]

as described in QA1719. Note that you can apply this key to a directory to prevent its contents from being backed up. The complete method from QA1719:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...