IOS--Do Not Backup属性
2012-07-03 14:36
274 查看
首先贴一段官网的代码!~下面英文的文档没删除,大家可以对比看,思路虽然清晰,但是翻译水平有限,点到为止!
Listing1 ExcludingaFilefromBackupsoniOS5.1
Listing2 SettingtheExtendedAttributeoniOS5.0.1
ItisnotpossibletoexcludedatafrombackupsoniOS5.0.IfyourappmustsupportiOS5.0,thenyouwillneedtostoreyourappdatain
avoidthatdatabeingbackedup.iOSwilldeleteyourfilesfromthe
whennecessary,soyourappwillneedtodegradegracefullyifit'sdatafilesaredeleted.
在IOS5.0中,不太可能取消data上传备份。如果你的app必须要支持IOS5.0,那么你需要将你的app数据存到Caches中防止上传备份。但是,在必要的时候,IOS会删掉caches目录。所以当你的datafiles被删除时,你的app要平稳的降级(最后一句没太看懂,知道大概意思)。
Youdonothavetoprepareyourappinanywayforbackupandrestoreoperations.DeviceswithanactiveiCloudaccounthavetheirappdatabackeduptoiCloudatappropriatetimes.Andfordevicesthatarepluggedintoacomputer,iTunesperformsanincremental
backupoftheapp’sdatafiles.However,iCloudandiTunesdonotbackupthecontentsofthefollowingdirectories:
简单的说上面那段话意思是说:你的app沙盒中的下面这些文件夹ICloud不会去备份!
<Application_Home>
<Application_Home>
<Application_Home>
Topreventthesyncingprocessfromtakingalongtime,beselectiveaboutwhereyouplacefilesinsideyourapp’shomedirectory.AppsthatstorelargefilescanslowdowntheprocessofbackinguptoiTunesoriCloud.Theseappscanalsoconsumealargeamount
ofauser'savailablestorage,whichmayencouragetheusertodeletetheappordisablebackupofthatapp'sdatatoiCloud.
Withthisinmind, youshouldstoreappdataaccordingtothefollowingguidelines:
考虑到上面那点,你必须遵守如下的规则去存储你的data:
Criticaldatashouldbestoredinthe <Application_Home>
dataisanydatathatcannotberecreatedbyyourapp,suchasuserdocumentsandotheruser-generatedcontent.重要的数据(不要由你的app再生的data)必须存储在 <Application_Home>
Supportfilesincludefilesyourapplicationdownloadsorgeneratesandthatyourapplicationcanrecreateasneeded.Thelocationforstoringyourapplication’ssupportfilesdependsonthecurrentiOSversion.支持文件(翻译过来很别扭,翻译水平比较弱,还是用英文吧)包括你的app下载的或产生的以及必要时候你的app可以再生的data。存储你的Support
files的位置取决于你当前IOS的版本。这个地方很纠结~网上看到很多的app因为这个被拒。你要支持5.0及以上,你就要处理三种情况,如下:
IniOS5.1andlater,storesupportfilesinthe <Application_Home>
andaddthe
tothecorresponding
(IfyouareusingCoreFoundation,addthe
toyour
ApplyingthisattributepreventsthefilesfrombeingbackeduptoiTunesoriCloud.Ifyouhavealargenumberofsupportfiles,youmaystoretheminacustomsubdirectoryandapplytheextendedattributetojustthedirectory.
在5.1及以后版本,存储Supportfiles在 <Application_Home>NSURLIsExcludedFromBackupKey这个属性,代码官网已经给了,上面的list1就是!添加这个属性防止你的文件被上传打iTunes或者iCloud如果你有大量的 support
files,你可以在这个目录下创建一个子目录,然后只对目录进行扩展属性就可以了。(翻译的有点纠结~点到为止)
IniOS5.0andearlier,storesupportfilesinthe <Application_Home>
topreventthemfrombeingbackedup.IfyouaretargetingiOS5.0.1,see How
doIpreventfilesfrombeingbackeduptoiCloudandiTunes? forinformationabouthowtoexcludefilesfrombackups.
在IOS5.0及以前前面已经说了,必要要存在 <Application_Home>
Cacheddatashouldbestoredinthe <Application_Home>
offilesyoushouldputinthe
usedbymagazine,newspaper,andmapapps.Yourappshouldbeabletogracefullyhandlesituationswherecacheddataisdeletedbythesystemtofreeupdiskspace.
缓存数据必要存储在 <Application_Home>
数据被系统删掉以释放硬盘空间的情况。
Temporarydatashouldbestoredinthe <Application_Home>
anydatathatyoudonotneedtopersistforanextendedperiodoftime.Remembertodeletethosefileswhenyouaredonewiththemsothattheydonotcontinuetoconsumespaceontheuser'sdevice.
临时文件应该存在 <Application_Home>
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; |
} |
#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; |
} |
Cachesto
avoidthatdatabeingbackedup.iOSwilldeleteyourfilesfromthe
Cachesdirectory
whennecessary,soyourappwillneedtodegradegracefullyifit'sdatafilesaredeleted.
在IOS5.0中,不太可能取消data上传备份。如果你的app必须要支持IOS5.0,那么你需要将你的app数据存到Caches中防止上传备份。但是,在必要的时候,IOS会删掉caches目录。所以当你的datafiles被删除时,你的app要平稳的降级(最后一句没太看懂,知道大概意思)。
AppBackupBestPractices
Youdonothavetoprepareyourappinanywayforbackupandrestoreoperations.DeviceswithanactiveiCloudaccounthavetheirappdatabackeduptoiCloudatappropriatetimes.Andfordevicesthatarepluggedintoacomputer,iTunesperformsanincrementalbackupoftheapp’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>
/Documentsdirectory.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/ApplicationSupportdirectory
andaddthe
attributeNSURLIsExcludedFromBackupKey
tothecorresponding
NSURLobjectusingthe
method.setResourceValue:forKey:error:
(IfyouareusingCoreFoundation,addthe
keykCFURLIsExcludedFromBackupKey
toyour
CFURLRefobjectusingthe
function.)CFURLSetResourcePropertyForKey
ApplyingthisattributepreventsthefilesfrombeingbackeduptoiTunesoriCloud.Ifyouhavealargenumberofsupportfiles,youmaystoretheminacustomsubdirectoryandapplytheextendedattributetojustthedirectory.
在5.1及以后版本,存储Supportfiles在 <Application_Home>
/Library/Application Support这个文件夹下,然后用
添加setResourceValue:forKey:error:函数,
files,你可以在这个目录下创建一个子目录,然后只对目录进行扩展属性就可以了。(翻译的有点纠结~点到为止)
IniOS5.0andearlier,storesupportfilesinthe <Application_Home>
/Library/Cachesdirectory
topreventthemfrombeingbackedup.IfyouaretargetingiOS5.0.1,see How
doIpreventfilesfrombeingbackeduptoiCloudandiTunes? forinformationabouthowtoexcludefilesfrombackups.
在IOS5.0及以前前面已经说了,必要要存在 <Application_Home>
/Library/Caches下面防止上传,但是会有随时被删除的可能~!如果你要支持IOS5.0.1,参见上面那个链接,将文件属性设置一下就可以了。具体代码如list2所示!上面已经给了。
Cacheddatashouldbestoredinthe <Application_Home>
/Library/Cachesdirectory.Examples
offilesyoushouldputinthe
Cachesdirectoryinclude(butarenotlimitedto)databasecachefilesanddownloadablecontent,suchasthat
usedbymagazine,newspaper,andmapapps.Yourappshouldbeabletogracefullyhandlesituationswherecacheddataisdeletedbythesystemtofreeupdiskspace.
缓存数据必要存储在 <Application_Home>
/Library/Caches目录下,例如下面这类文件应该放在Caches文件下,数据库缓存文件,可下载内容数据,比如应在杂志、报纸、和地图的app等等。你的app必须要处理好当cached
数据被系统删掉以释放硬盘空间的情况。
Temporarydatashouldbestoredinthe <Application_Home>
/tmpdirectory.Temporarydatacomprises
anydatathatyoudonotneedtopersistforanextendedperiodoftime.Remembertodeletethosefileswhenyouaredonewiththemsothattheydonotcontinuetoconsumespaceontheuser'sdevice.
临时文件应该存在 <Application_Home>
/tmp目录下,临时文件包括哪些你不需要保存很长时间的data,但是你要记住,在你对他完成操作的时候,记得删掉临时文件,免得让这些临时数据消耗用户的空间。
相关文章推荐
- OS--阻止文件被iTunes和iCloud同步,Do Not Backup属性
- 【每天总结一个ios开发心得】对于Retain和Assign属性的理解
- iOS开发备忘录:属性列表文件数据持久化
- iOS中的@property的属性
- iOS的文件分类、存放路径及文件属性
- 判断一个类是否支持某属性和方法以及如何判断IOS的版本
- ios的属性和成员变量在.h文件和.m文件的不同
- iOS开发中@property的属性介绍
- 【深入浅出ios开发】数据存储-----XML属性列表(plist)归档
- iosUITextField属性
- iOS之NSAttributedString-------字符属性
- ios layer shadow 属性设置
- iOS中UIScrollView属性
- ios UIView的clipsTobounds属性
- 如何应用autosizing属性使iOS应用程序界面适配iPhone5
- iOS的@property属性和@synthesize属性
- iOS中属性与成员变量的区别
- iOS开发 automaticallyAdjustsScrollViewInsets 属性设置
- iOS开发3:UITextField控件的属性
- [iOS 通过class-dump] 逆向出方法名/属性列表