public
Last active

Setting the do not backup attribute in different iOS versions

  • Download Gist
NSFileManager+DoNotBackup.h
Objective-C
1 2 3 4 5
@interface NSFileManager (DoNotBackup)
 
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL;
 
@end
NSFileManager+DoNotBackup.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#import "NSFileManager+DoNotBackup.h"
#include <sys/xattr.h>
 
@implementation NSFileManager (DoNotBackup)
 
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
} else { // iOS >= 5.1
NSError *error = nil;
[URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
return error == nil;
}
}
 
@end
NSFileManagerDoNotBackup.h
Objective-C
1 2 3 4 5
@interface NSFileManager (DoNotBackup)
 
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL;
 
@end
NSFileManagerDoNotBackup.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#import "NSFileManager+DoNotBackup.h"
#include <sys/xattr.h>
 
@implementation NSFileManager (DoNotBackup)
 
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
} else { // iOS >= 5.1
return [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil];
}
}
 
@end

please let me know how to use this function in my application ??

Usage Example:

[[NSFileManager defaultManager] addSkipBackupAttributeToItemAtURL:yourFileNSURL];

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.