Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Setting the do not backup attribute in different iOS versions

View NSFileManager+DoNotBackup.h
1 2 3 4 5
@interface NSFileManager (DoNotBackup)
 
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL;
 
@end
View NSFileManager+DoNotBackup.h
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
View NSFileManager+DoNotBackup.h
1 2 3 4 5
@interface NSFileManager (DoNotBackup)
 
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL;
 
@end
View NSFileManager+DoNotBackup.h
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];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.