NSURLIsExcludedFromBackupKey无法正确设置 [英] NSURLIsExcludedFromBackupKey can not be set correctly

查看:217
本文介绍了NSURLIsExcludedFromBackupKey无法正确设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阻止整个文件夹由itunes备份。我按照技术报告 http://developer.apple.com/library /ios/#qa/qa1719/_index.html 但似乎falg每次都是零。我在模拟器和设备上使用IOS 5.1。但没有任何帮助。该方法返回 成功 每一次,但标志仍是零

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

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

if(!success){

NSLog(@错误排除%@来自备份%@,[URL lastPathComponent],错误);
返回false;
}

if(!flag)
返回false;

return [flag boolValue];
}

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

BOOL成功= [URL setResourceValue:[NSNumber的numberWithBool:YES]
forKey:NSURLIsExcludedFromBackupKey误差:&安培;错误];

if(!success){

NSLog(@错误排除%@来自备份%@,[URL lastPathComponent],错误);

}

返回成功;

}

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

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

if(!success){

NSLog(@错误排除%@来自备份%@,[URL lastPathComponent],错误);

}

返回成功;

}


解决方案

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





假设您有一个NSString *路径 - 文件/文件夹的路径,不要使用调用方法:

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



相反用[NSURL fileURLWithPath:路径];



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



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



希望这会有所帮助。



问候,
George


I am trying to prevent a whole folder from beeing backed up by itunes. I followed the Technical Report http://developer.apple.com/library/ios/#qa/qa1719/_index.html But it seems that falg is everytime nil. I use IOS 5.1 tried in in simulator and on device. But nothing helps. The methods return "success" every time, but flag is still nil.

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

}

解决方案

I just fixed this in my app and while it was kind of frustrating , it all ended up working great. So , here's the code for the addSkipBackupAttributeToItemAtURL . You may want to check that out. It treats 5.0.1 and 5.0 too. You are only treating 5.1 and higher in your code.

BUT:

Let's say you have an NSString *path - the path to your file /folder , don't call the method with:

[NSURL urlWithString:path]; It will work on 5.0.1 but not on 5.1 and higher.

Instead use [NSURL fileURLWithPath:path];

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

In fact , I think that is the only problem with your code. Taking the method I linked to , will only provide backward compatibility , which is a great addition.

Hope this helps.

Regards, George

这篇关于NSURLIsExcludedFromBackupKey无法正确设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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