Apple对应用程序放在沙盒中的文件有严格要求,主要有:
存放位置要求
用户创建的文件,(程序不能自动生成的),需要放在Documents\
缓存文件,需要放在Library\Caches\
临时文件,放在tmp\,而且要注意清空
文件备份
这个可以通过设置文件的一个属性来控制,具体见下面代码
除了用户创建和编辑的文件,不允许保存到iTunes和iCloud
用户升级程序之后,所有Documents\和Library\的文件会自动复制到新的bundle中去
下面的代码是如何设置属性,让apple在备份的时候,不会包含这个文件。
对于iOS版本5.1之前和之后的处理方式是不一样的。
+(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
if (![[NSFileManager defaultManager] fileExistsAtPath: [URL path]]) {
return NO;
}
if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.1) {
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;
} else {
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;
}
}