我应该在哪里保存数据 &我想长期保留的文件,以及如何防止 iCloud 备份它们 [英] Where should I save data & files I want to keep long term, and how do I prevent iCloud from backing them up

查看:9
本文介绍了我应该在哪里保存数据 &我想长期保留的文件,以及如何防止 iCloud 备份它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件 - 包括核心数据存储库在内的任意类型 - 我需要保留这些文件,而不是让 iOS 删除它们.通常,我不希望 iCloud 备份这些文件.我应该在哪里保存它们?

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?

推荐答案

从 iOS5.0 及更早版本、5.0.1、5.1 及更高版本开始,本地保存文件主要是为了解决 iCloud 备份问题.有两个 Apple 源文档(文件系统编程指南QA1719) 共同提供支持以下内容的信息:

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

文件应保存在Caches"目录中,因为如果它们存储在 Documents 文件夹中,则无法防止备份.请注意,系统可能会删除这些文件(请参阅 QA1719),因此您需要能够根据需要重新创建每个文件.要查找缓存目录,请使用:

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
  • 文件应保存在/Library/Application Support"中(FSP,第 15 页),可以通过以下方式更好地访问:

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

    保存到这个目录(或子目录)的文件需要一个扩展属性来告诉 iCloud 不要备份它们(参见 QA1719).

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

    PS:我还没有找到将部署目标设置为此版本的方法,如果有方法请发表评论.

    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

    文件(或文件文件夹)应位于如上所述的应用程序支持"文件夹中.防止 iCloud 备份使用:

    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]
    

    如 QA1719 中所述.请注意,您可以将此密钥应用于目录以防止备份其内容.来自QA1719的完整方法:

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

    这篇关于我应该在哪里保存数据 &我想长期保留的文件,以及如何防止 iCloud 备份它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆