iphone - 无法正确设置 NSURLIsExcludedFromBackupKey

标签 iphone ios

我试图阻止整个文件夹被 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;

}

最佳答案

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

但是:

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

[NSURL urlWithString:path]; 它可以在 5.0.1 上运行,但不能在 5.1 及更高版本上运行。

相反,使用 [NSURL fileURLWithPath:path];

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

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

希望这对您有所帮助。

问候, 乔治

关于iphone - 无法正确设置 NSURLIsExcludedFromBackupKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10836134/

相关文章:

iphone - iOS : Call a method just one time

iphone - Google feed api 与 iPhone/Objective C 集成

ios - [NSPlaceholderString initWithString :]: nil argument on jail break device

ios - UIButton 每次点击时不会出现 "blink"

ios - 使用 setText 时未调用 UITextField drawPlaceholderInRect

iphone - 将 UITabBarItem 添加到 UIViewController 中的 UITabBar

iphone - IOS:以编程方式创建 UI 或使用 Interface builder

iphone - iOS/Objective-C - 如何从 iPhone 应用程序查询 Web 服务器数据库?

ios - xml SOAP 与 swift (IOS 8)

ios - 嵌套 Collection View Swift 4.2