我应该在哪里保存我想长期保存的数据和文件,以及如何阻止iCloud备份它们

时间:2023-01-13 10:13:01

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?

我有任意类型的文件,包括核心数据存储库 - 我需要保留并且没有iOS删除它们。通常,我不希望iCloud支持这些文件。我应该在哪里保存它们?

1 个解决方案

#1


17  

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:

本地保存的文件从iOS5.0及更早版本,5.0.1和5.1更新,主要更新为地址iCloud备份问题。有两个Apple源文档(文件系统编程指南和QA1719),它们共同提供支持以下内容的信息:

  • 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:

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

[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:

文件应保存在'/ Library / Application Support'(FSP,第15页)中,可通过以下方式更好地访问:

[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).

保存到此目录(或子目录)的文件需要一个扩展属性来告诉iCloud不要备份它们(参见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.

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

  • 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:

文件(或文件夹)应位于“应用程序支持”文件夹中,如上所述。为了防止iCloud备份使用:

[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:

如QA1719中所述。请注意,您可以将此密钥应用于目录以防止其内容被备份。 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;
}

#1


17  

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:

本地保存的文件从iOS5.0及更早版本,5.0.1和5.1更新,主要更新为地址iCloud备份问题。有两个Apple源文档(文件系统编程指南和QA1719),它们共同提供支持以下内容的信息:

  • 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:

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

[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:

文件应保存在'/ Library / Application Support'(FSP,第15页)中,可通过以下方式更好地访问:

[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).

保存到此目录(或子目录)的文件需要一个扩展属性来告诉iCloud不要备份它们(参见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.

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

  • 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:

文件(或文件夹)应位于“应用程序支持”文件夹中,如上所述。为了防止iCloud备份使用:

[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:

如QA1719中所述。请注意,您可以将此密钥应用于目录以防止其内容被备份。 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;
}