Apple對應(yīng)用程序放在沙盒中的文件有嚴格要求贫母,主要有:
存放位置要求
用戶創(chuàng)建的文件文兑,(程序不能自動生成的),需要放在Documents\
緩存文件腺劣,需要放在Library\Caches\
臨時文件绿贞,放在tmp\,而且要注意清空
文件備份
這個可以通過設(shè)置文件的一個屬性來控制誓酒,具體見下面代碼
除了用戶創(chuàng)建和編輯的文件樟蠕,不允許保存到iTunes和iCloud
用戶升級程序之后,所有Documents\和Library\的文件會自動復(fù)制到新的bundle中去
下面的代碼是如何設(shè)置屬性靠柑,讓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;
}
}