蘋果在iOS 5系統(tǒng)時(shí),,對(duì)app的文件存儲(chǔ)提出了新的要求,。從它的guildline來看,,是推薦開發(fā)者盡量把a(bǔ)pp生成的文件放在Caches目錄下的。原文如下: Only user-generated data or that cannot otherwise be recreated by your application, should be stored in the /Documents directory and rest should be stored to /Library/Caches directory。
照做會(huì)怎么樣,?如果這么做的話,,會(huì)出現(xiàn)兩種情況 - 如果對(duì)此置之不理,繼續(xù)把應(yīng)用生成的文件放在Documents目錄下,,那么這些文件會(huì)被備份到iTunes或者iCloud,。如果這些文件很大,那么用戶可能需要為了同步消耗不少流量,,然后蘋果可能會(huì)因此拒絕你的應(yīng)用上架,,這是一個(gè)悲劇。
- 如果開發(fā)者照Apple說的干,,把應(yīng)用生成的文件放在Caches目錄下,,那么蘋果不會(huì)拒絕你的應(yīng)用,很happy,。但是iOS 5會(huì)在磁盤空間緊張的時(shí)候刪除Caches目錄下的文件,,這對(duì)用戶來說可能是一個(gè)更大的悲劇。
如何應(yīng)對(duì)新的文件存儲(chǔ)策略?開發(fā)者在這時(shí)陷入了兩難的境地,,但是到了iOS 5.0.1的時(shí)候,,開發(fā)者多了第三種選擇: - 繼續(xù)把文件存儲(chǔ)在Documents目錄下,但是標(biāo)記這些文件為不需要備份,。詳情請(qǐng)參考 technote (QA1719)
原文如下: Q: My app has a number of files that need to be stored on the device permanently for my app to function properly offline. However, those files do not contain user data and don’t need to be backed up. How should I store those files in iOS 5? A: Starting in iOS 5.0.1 a new “do not back up” file attribute has been introduced allowing developers to clearly specify which files should be backed up, which files are local caches only and subject to purge, and which files should not be backed up but should also not be purged. In addition, setting this attribute on a folder will prevent the folder and all of its contents from being backed up.
代碼示例給文件加上”do not back up”屬性的代碼如下,,需要注意這個(gè)是iOS 5.0.1才有效,低于這個(gè)版本就別費(fèi)勁了,。 #include <sys/xattr.h>
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
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;
}
|