在iOS 5及以前,如果启用了iCloud或iTunes备份文件的话,Documents目录下的所有文件都会被备份,并且不能设置。
但是从iOS 5.0.1开始,Apple提供了将不需要备份的文件排除在外的方法,通过设置文件属性来确定那些文件是不需要备份的。
iOS 5.0.1时:
#import <sys/xattr.h>
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
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;
}
iOS 5.1时:
- (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;
}
发表评论
要发表评论,您必须先登录。