您的位置:首页 > 移动开发 > IOS开发

IOS--Do Not Backup属性

2012-07-03 14:36 274 查看
首先贴一段官网的代码!~下面英文的文档没删除,大家可以对比看,思路虽然清晰,但是翻译水平有限,点到为止!

Listing1  ExcludingaFilefromBackupsoniOS5.1

-(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL*)URL

{

assert([[NSFileManagerdefaultManager]fileExistsAtPath:[URLpath]]);


NSError*error=nil;

BOOLsuccess=[URLsetResourceValue:[NSNumbernumberWithBool:YES]

forKey:NSURLIsExcludedFromBackupKeyerror:&error];

if(!success){

NSLog(@"Errorexcluding%@frombackup%@",[URLlastPathComponent],error);

}

returnsuccess;

}

Listing2  SettingtheExtendedAttributeoniOS5.0.1

#import<sys/xattr.h>

-(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL*)URL

{

assert([[NSFileManagerdefaultManager]fileExistsAtPath:[URLpath]]);


constchar*filePath=[[URLpath]fileSystemRepresentation];


constchar*attrName="com.apple.MobileBackup";

u_int8_tattrValue=1;


intresult=setxattr(filePath,attrName,&attrValue,sizeof(attrValue),0,0);

returnresult==0;

}

ItisnotpossibletoexcludedatafrombackupsoniOS5.0.IfyourappmustsupportiOS5.0,thenyouwillneedtostoreyourappdatain 
Caches
 to
avoidthatdatabeingbackedup.iOSwilldeleteyourfilesfromthe
Caches
 directory
whennecessary,soyourappwillneedtodegradegracefullyifit'sdatafilesaredeleted.

在IOS5.0中,不太可能取消data上传备份。如果你的app必须要支持IOS5.0,那么你需要将你的app数据存到Caches中防止上传备份。但是,在必要的时候,IOS会删掉caches目录。所以当你的datafiles被删除时,你的app要平稳的降级(最后一句没太看懂,知道大概意思)。


AppBackupBestPractices

Youdonothavetoprepareyourappinanywayforbackupandrestoreoperations.DeviceswithanactiveiCloudaccounthavetheirappdatabackeduptoiCloudatappropriatetimes.Andfordevicesthatarepluggedintoacomputer,iTunesperformsanincremental
backupoftheapp’sdatafiles.However,iCloudandiTunesdonotbackupthecontentsofthefollowingdirectories:

简单的说上面那段话意思是说:你的app沙盒中的下面这些文件夹ICloud不会去备份!

<Application_Home>
/
AppName
.app


<Application_Home>
/Library/Caches


<Application_Home>
/tmp


Topreventthesyncingprocessfromtakingalongtime,beselectiveaboutwhereyouplacefilesinsideyourapp’shomedirectory.AppsthatstorelargefilescanslowdowntheprocessofbackinguptoiTunesoriCloud.Theseappscanalsoconsumealargeamount
ofauser'savailablestorage,whichmayencouragetheusertodeletetheappordisablebackupofthatapp'sdatatoiCloud.

 Withthisinmind, youshouldstoreappdataaccordingtothefollowingguidelines:

考虑到上面那点,你必须遵守如下的规则去存储你的data:

Criticaldatashouldbestoredinthe <Application_Home>
/Documents
 directory.Critical
dataisanydatathatcannotberecreatedbyyourapp
,suchasuserdocumentsandotheruser-generatedcontent.重要的数据(不要由你的app再生的data)必须存储在 <Application_Home>
/Documents
 文件夹下。

Supportfilesincludefilesyourapplicationdownloadsorgeneratesandthatyourapplicationcanrecreateasneeded.Thelocationforstoringyourapplication’ssupportfilesdependsonthecurrentiOSversion.支持文件(翻译过来很别扭,翻译水平比较弱,还是用英文吧)包括你的app下载的或产生的以及必要时候你的app可以再生的data。存储你的Support
files的位置取决于你当前IOS的版本。这个地方很纠结~网上看到很多的app因为这个被拒。你要支持5.0及以上,你就要处理三种情况,如下:

IniOS5.1andlater,storesupportfilesinthe <Application_Home>
/Library/ApplicationSupport
 directory
andaddthe
NSURLIsExcludedFromBackupKey
 attribute
tothecorresponding 
NSURL
 objectusingthe 
setResourceValue:forKey:error:
 method.
(IfyouareusingCoreFoundation,addthe 
kCFURLIsExcludedFromBackupKey
 key
toyour 
CFURLRef
 objectusingthe 
CFURLSetResourcePropertyForKey
 function.)
ApplyingthisattributepreventsthefilesfrombeingbackeduptoiTunesoriCloud.Ifyouhavealargenumberofsupportfiles,youmaystoretheminacustomsubdirectoryandapplytheextendedattributetojustthedirectory.

在5.1及以后版本,存储Supportfiles在 <Application_Home>
/Library/Application
Support
 这个文件夹下,然后用 
setResourceValue:forKey:error:函数,
添加NSURLIsExcludedFromBackupKey这个属性,代码官网已经给了,上面的list1就是!添加这个属性防止你的文件被上传打iTunes或者iCloud如果你有大量的 support
files,你可以在这个目录下创建一个子目录,然后只对目录进行扩展属性就可以了。(翻译的有点纠结~点到为止)

IniOS5.0andearlier,storesupportfilesinthe <Application_Home>
/Library/Caches
 directory
topreventthemfrombeingbackedup.IfyouaretargetingiOS5.0.1,see How
doIpreventfilesfrombeingbackeduptoiCloudandiTunes? forinformationabouthowtoexcludefilesfrombackups.  

 
在IOS5.0及以前前面已经说了,必要要存在 <Application_Home>
/Library/Caches
下面防止上传,但是会有随时被删除的可能~!如果你要支持IOS5.0.1,参见上面那个链接,将文件属性设置一下就可以了。具体代码如list2所示!上面已经给了。

Cacheddatashouldbestoredinthe <Application_Home>
/Library/Caches
 directory.Examples
offilesyoushouldputinthe 
Caches
 directoryinclude(butarenotlimitedto)databasecachefilesanddownloadablecontent,suchasthat
usedbymagazine,newspaper,andmapapps.Yourappshouldbeabletogracefullyhandlesituationswherecacheddataisdeletedbythesystemtofreeupdiskspace.

缓存数据必要存储在 <Application_Home>
/Library/Caches
 目录下,例如下面这类文件应该放在Caches文件下,数据库缓存文件,可下载内容数据,比如应在杂志、报纸、和地图的app等等。你的app必须要处理好当cached
数据被系统删掉以释放硬盘空间的情况。

Temporarydatashouldbestoredinthe <Application_Home>
/tmp
 directory.Temporarydatacomprises
anydatathatyoudonotneedtopersistforanextendedperiodoftime.Remembertodeletethosefileswhenyouaredonewiththemsothattheydonotcontinuetoconsumespaceontheuser'sdevice.

临时文件应该存在 <Application_Home>
/tmp
目录下,临时文件包括哪些你不需要保存很长时间的data,但是你要记住,在你对他完成操作的时候,记得删掉临时文件,免得让这些临时数据消耗用户的空间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息