【问题标题】:NSURLIsExcludedFromBackupKey can not be set correctlyNSURLIsExcludedFromBackupKey 无法正确设置
【发布时间】:2012-06-05 20:38:11
【问题描述】:

我试图阻止整个文件夹被 iTunes 备份。我关注了技术报告http://developer.apple.com/library/ios/#qa/qa1719/_index.html 但似乎 falg 每次都为零。我使用在模拟器和设备上试用过的 IOS 5.1。但没有任何帮助。这些方法每次都返回“成功”,但标志仍然为零。

+ (BOOL) hasSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    NSError *error = nil;

    id flag = nil;
    BOOL success = [URL getResourceValue: &flag
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];

    if(!success){

        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
        return false;
    }

    if (!flag)
        return false;

    return [flag boolValue];
}

+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    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;

}

+ (BOOL)removeSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    NSError *error = nil;

    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: NO]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];

    if(!success){

        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);

    }

    return success;

}

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    我刚刚在我的应用程序中修复了这个问题,虽然有点令人沮丧,但最终一切都很好。 所以,这里是 addSkipBackupAttributeToItemAtURL 的代码。你可能想检查一下。它也处理 5.0.1 和 5.0。您只在代码中处理 5.1 及更高版本。

    但是:

    假设你有一个 NSString *path - 你的文件 /folder 的路径,不要用调用方法:

    [NSURL urlWithString:path]; 它适用于 5.0.1,但不适用于 5.1 及更高版本。

    改为use [NSURL fileURLWithPath:path];

    所以:[MyClass addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];

    事实上,我认为这是您的代码的唯一问题。采用我链接的方法,只会提供向后兼容性,这是一个很好的补充。

    希望这会有所帮助。

    问候, 乔治

    【讨论】:

    • 你救了我的早晨太棒了!
    • 谢谢!!! FWIW,我向 Apple 提交了一个错误报告......如果它不起作用,它至少应该给出一个错误。
    • 嗯,这绝对不是特殊的和奇怪的或任何东西......非常感谢你的回答!!!完全为我节省了很多悲伤。
    • 在我的应用程序部署目标是 6.0,该区域拒绝应用程序,因为我正在使用 [NSURL urlWithString:path];而不是 [NSURL fileURLWithPath:path]
    【解决方案2】:

    对我来说同样的问题。 我还解决了它,改变了我调用 addSkipBackupAttributeToItemAtURL 的方式 这是正确的方法:

    [MyClass addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
    

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 2012-05-19
      • 2015-05-29
      • 2017-01-03
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多