15

我的文档目录中有一个巨大的文件和目录树用于缓存。

按照建议,我将NSURLIsExcludedFromBackupKey用于阻止 iTunes 通过应用程序保存这些数据。

我可以在我的根目录 URL 上使用它一次吗?

[rootDirectoryURL setResourceValue:[NSNumber numberWithBool:YES] forKey:@"NSURLIsExcludedFromBackupKey" error:&error];

还是我必须为每个文件调用它?

4

3 回答 3

26

是的,您可以将您想要排除的目录的 NSURL 传递给它。

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

如果有疑问,您可以使用

id flag = nil;
[URL getResourceValue: &flag
               forKey: NSURLIsExcludedFromBackupKey error: &error];
NSLog (@"NSURLIsExcludedFromBackupKey flag value is %@", flag)
于 2013-01-17T10:29:08.870 回答
1

从我的测试(在模拟器上)来看,它不是递归的,而且由于 Documents 目录中的数据过多,我刚刚被应用程序拒绝。直接在 Documents 目录中的每个文件夹都被标记,所以我猜它们里面的文件没有,即使在真实设备上也是如此。但是我还在文件夹中添加了新内容,所以我可能只需要再次添加标志。

所以我现在使用这种递归方法,并在每个添加的新文件上添加标志:

- (void) addSkipBackupAttributeToFolder:(NSURL*)folder
{
    [self addSkipBackupAttributeToItemAtURL:folder];

    NSError* error = nil;
    NSArray* folderContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[folder path] error:&error];

    for (NSString* item in folderContent)
    {
        NSString* path = [folder.path stringByAppendingPathComponent:item];
        [self addSkipBackupAttributeToFolder:[NSURL fileURLWithPath:path]];
    }
}
于 2014-10-31T21:06:02.703 回答
-1

这是已接受答案的快速版本:

func addSkipBackupAttributeToItemAtURL(URL: NSURL) -> Bool {
    assert(NSFileManager.defaultManager().fileExistsAtPath(URL.path!))
    var success: Bool = true

    do {
        try URL.setResourceValue(NSNumber(bool: true), forKey: NSURLIsExcludedFromBackupKey)
    } catch {
        success = false
        print("Error excluding \(URL.lastPathComponent) from backup: \(error)")
    }

    return success
}
于 2016-09-08T08:50:04.373 回答