Exclude from iCloud backup: NSURLIsExcludedFromBackupKey

雨燕双飞 提交于 2019-12-03 03:46:35
Gujamin

I think the trick is to add the NSURLIsExcludedFromBackupKey OR make sure the directory is outside the documents directory. I did this by moving my documents to the Library/Application Support folder (since it didn't make sense in the /tmp or /Caches folders):

- (void)createNewRefFolder
{
    NSError *error;

    // store in /Library/Application Support/BUNDLE_IDENTIFIER/Reference
    // make sure Application Support folder exists
    NSURL *applicationSupportDirectory = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
                                                                                inDomain:NSUserDomainMask
                                                                       appropriateForURL:nil
                                                                                  create:YES
                                                                                   error:&error];

    if (error) {
        NSLog(@"KCDM: Could not create application support directory. %@", error);
    }

    NSURL *referenceFolder = [applicationSupportDirectory URLByAppendingPathComponent:@"Reference" isDirectory:YES];
    if (![[NSFileManager defaultManager] createDirectoryAtPath:[referenceFolder path]
                                   withIntermediateDirectories:YES
                                                    attributes:nil
                                                         error:&error]) {
        NSLog(@"KCDM: Error creating Reference folder: %@ ...", error);
    }

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

Little UPDATE from previous answers.

You have to check the existence of the file. Otherwise, you will get this error,

Error excluding [FileName] from backup: 
Error Domain=NSCocoaErrorDomain Code=4 "The file “[FileName]” doesn’t exist." 
 ... 

I am not sure if we should check for the value is already updated or not. e.g. if the API reset already set value or not. If it tries to update the file system again for a set value that is more time consuming, I guess.

Updated method...

+ (BOOL)addSkipBackupAttributeToURLAtPath:(NSURL *)url
{
    if (!url) return NO;
    if (![[NSFileManager defaultManager] fileExistsAtPath:url.path]) return NO;

    NSError *error = nil;
    NSNumber *value = nil;
    BOOL success = [url getResourceValue:&value forKey:NSURLIsExcludedFromBackupKey error:&error];
    if (value.boolValue == YES) return YES;

    success = [url setResourceValue:[NSNumber numberWithBool:YES]
                                  forKey:NSURLIsExcludedFromBackupKey error:&error];
    if(!success){
        NSLog(@"Error excluding %@ from backup: %@", [url lastPathComponent], error);
    }
    return success;
}

+ (BOOL)addSkipBackupAttributeToFileAtPath:(NSString *)path
{
    if (!path) return NO;
    return [self addSkipBackupAttributeToURLAtPath:[NSURL fileURLWithPath:path]];
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!