Created
March 8, 2012 09:42
-
-
Save tibr/1999985 to your computer and use it in GitHub Desktop.
Setting the do not backup attribute in different iOS versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSFileManager (DoNotBackup) | |
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSFileManager (DoNotBackup) | |
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Usage Example:
[[NSFileManager defaultManager] addSkipBackupAttributeToItemAtURL:yourFileNSURL];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please let me know how to use this function in my application ??