Objective C:
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
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;
}
Swift 3.0:
func addSkipBackupAttributeToItemAtURL(filePath:String) -> Bool{
let URL:NSURL = NSURL.fileURL(withPath: filePath) as NSURL
assert(FileManager.default.fileExists(atPath: filePath), "File \(filePath) does not exist")
var success: Bool
do {
try URL.setResourceValue(true, forKey:URLResourceKey.isExcludedFromBackupKey)
success = true
} catch let error as NSError {
success = false
print("Error excluding \(URL.lastPathComponent) from backup \(error)");
}
return success
}