Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLIsExcludedFromBackupKey can not be set correctly

Tags:

ios

iphone

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;

}
like image 973
kaeLum Avatar asked May 31 '12 15:05

kaeLum


2 Answers

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

like image 52
George Avatar answered Nov 19 '22 18:11

George


Same problem for me. I also solved it changing the way I called addSkipBackupAttributeToItemAtURL That's the correct way:

[MyClass addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
like image 44
user1285413 Avatar answered Nov 19 '22 18:11

user1285413