Eugene Biryukov
Eugene Biryukov

Reputation: 1712

NSURLIsExcludedFromBackupKey does not work on iOS 8

I use standard method for excluding from backup:

+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath:[URL path]]);

    NSError *error = nil;
    BOOL success = [URL setResourceValue:@(YES)
                                  forKey:NSURLIsExcludedFromBackupKey error:&error];
    if (!success) {
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }

    return success;
} 

As some people suggested I use this method with [NSURL fileURLWithPath:path] but it doesn't work:

Error excluding groups.xml from backup Error Domain=NSCocoaErrorDomain Code=513 "Не удалось завершить операцию. (Cocoa, ошибка 513)"

UserInfo=0x170272ac0 {

NSURL=file:///private/var/mobile/Containers/Bundle/Application/59286088-C226-4653-A84F-A4B5D40C11DE/anatomyfree.app/groups.xml, NSFilePath=/private/var/mobile/Containers/Bundle/Application/59286088-C226-4653-A84F-A4B5D40C11DE/anatomyfree.app/groups.xml, NSUnderlyingError=0x1702492a0 "Не удалось завершить операцию. Operation not permitted" }

This file is exist. It is gets parsed but it can't be excluded from backup. What's reason of it?

I use iOS 8.2, iPhone 6, ru locale.

Here is method call:

NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSURL *url = [NSURL fileURLWithPath:path];
[Utilities addSkipBackupAttributeToItemAtURL:url];

Upvotes: 0

Views: 652

Answers (4)

Ben
Ben

Reputation: 28

The reason why it is not working is because your resource file is shipped with your app (bundle), which do not and never backup in user's iCloud.

Resources located in your app bundle is never written, that's why any write/modify operation will fail.

In case you really want to work with iCloud backup, you need to first copy the file into directory like NSSearchPathDirectory.DocumentDirectory.

Upvotes: 0

MindSpiker
MindSpiker

Reputation: 1474

I was had a problem using:

pDirURL.setValue(true , forKey: NSURLIsExcludedFromBackupKey)

Which was working fine. One day it started throwing NSUnknownKeyException for unknown reasons. Now I use

try pDirURL.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)

which works fine again!

Upvotes: 0

vichevstefan
vichevstefan

Reputation: 898

Add this code before NSURL *url = [NSURL fileURLWithPath:path]

if([[UIDevice currentDevice] systemVersion] floatValue] >= 8.0f){
    path = [NSString stringWithFormat:@"./%@",path];
}

Upvotes: 0

Nikita Arkhipov
Nikita Arkhipov

Reputation: 1258

Try this, it works perfectly in iOS 8 for me:

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.%@", documentsPath, fileName, extension]];
[Utilities addSkipBackupAttributeToItemAtURL:url];

Upvotes: 0

Related Questions