首先贴一段官网的代码!~下面英文的文档没删除,大家可以对比看,思路虽然清晰,但是翻译水平有限,点到为止!
Excluding a File from Backups on iOS 5.1
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL |
{ |
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]); |
|
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; |
} |
Setting the Extended Attribute on iOS 5.0.1
#import <sys/xattr.h> |
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL |
{ |
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]); |
|
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; |
} |
Caches
to avoid that data being backed up. iOS will delete your files from theCaches
directory when necessary, so your app will need to degrade gracefully if it's data files are deleted.
在IOS5.0中,不太可能取消data上传备份。如果你的app必须要支持IOS5.0,那么你需要将你的app数据存到Caches中防止上传备份。但是,在必要的时候,IOS会删掉caches目录。所以当你的data files被删除时,你的app要平稳的降级(最后一句没太看懂,知道大概意思)。
You do not have to prepare your app in any way for backup and restore operations. Devices with an active iCloud account have their app data backed up to iCloud at appropriate times. And for devices that are plugged into a computer, iTunes performs an incremental backup of the app’s data files. However, iCloud and iTunes do not back up the contents of the following directories:
简单的说上面那段话意思是说:你的app沙盒中的下面这些文件夹ICloud不会去备份!
<Application_Home>/
AppName.app
<Application_Home>/Library/Caches
<Application_Home>/tmp
To prevent the syncing process from taking a long time, be selective about where you place files inside your app’s home directory. Apps that store large files can slow down the process of backing up to iTunes or iCloud. These apps can also consume a large amount of a user's available storage, which may encourage the user to delete the app or disable backup of that app's data to iCloud.
With this in mind, you should store app data according to the following guidelines:
考虑到上面那点,你必须遵守如下的规则去存储你的data:
Critical data should be stored in the <Application_Home>/Documents
directory. Critical data is any data that cannot be recreated by your app, such as user documents and other user-generated content.重要的数据(不要由你的app再生的data)必须存储在 <Application_Home>/Documents
文件夹下。
Support files include files your application downloads or generates and that your application can recreate as needed. The location for storing your application’s support files depends on the current iOS version.支持文件(翻译过来很别扭,翻译水平比较弱,还是用英文吧)包括你的app下载的或产生的以及必要时候你的app可以再生的data。存储你的Support files的位置取决于你当前IOS的版本。这个地方很纠结~网上看到很多的app因为这个被拒。你要支持5.0及以上,你就要处理三种情况,如下:
In iOS 5.1 and later, store support files in the <Application_Home>/Library/Application Support
directory and add theNSURLIsExcludedFromBackupKey
attribute to the corresponding NSURL
object using the setResourceValue:forKey:error:
method. (If you are using Core Foundation, add the kCFURLIsExcludedFromBackupKey
key to your CFURLRef
object using the CFURLSetResourcePropertyForKey
function.) Applying this attribute prevents the files from being backed up to iTunes or iCloud. If you have a large number of support files, you may store them in a custom subdirectory and apply the extended attribute to just the directory.
在5.1及以后版本,存储Support files在 <Application_Home>/Library/Application Support
这个文件夹下,然后用 setResourceValue:forKey:error:函数,
添加NSURLIsExcludedFromBackupKey这个属性,代码官网已经给了,上面的list1就是!添加这个属性防止你的文件被上传打iTunes或者iCloud如果你有大量的 support files,你可以在这个目录下创建一个子目录,然后只对目录进行扩展属性就可以了。(翻译的有点纠结~点到为止)
In iOS 5.0 and earlier, store support files in the <Application_Home>/Library/Caches
directory to prevent them from being backed up. If you are targeting iOS 5.0.1, see How do I prevent files from being backed up to iCloud and iTunes? for information about how to exclude files from backups.
在IOS5.0及以前前面已经说了,必要要存在 <Application_Home>
/Library/Caches
下面防止上传,但是会有随时被删除的可能~!如果你要支持IOS5.0.1,参见上面那个链接,将文件属性设置一下就可以了。具体代码如list2所示!上面已经给了。 Cached data should be stored in the <Application_Home>/Library/Caches
directory. Examples of files you should put in the Caches
directory include (but are not limited to) database cache files and downloadable content, such as that used by magazine, newspaper, and map apps. Your app should be able to gracefully handle situations where cached data is deleted by the system to free up disk space.
缓存数据必要存储在 <Application_Home>/Library/Caches
目录下,例如下面这类文件应该放在Caches文件下,数据库缓存文件,可下载内容数据,比如应在杂志、报纸、和地图的app等等。你的app必须要处理好当cached 数据被系统删掉以释放硬盘空间的情况。
Temporary data should be stored in the <Application_Home>/tmp
directory. Temporary data comprises any data that you do not need to persist for an extended period of time. Remember to delete those files when you are done with them so that they do not continue to consume space on the user's device.
临时文件应该存在 <Application_Home>/tmp
目录下,临时文件包括哪些你不需要保存很长时间的data,但是你要记住,在你对他完成操作的时候,记得删掉临时文件,免得让这些临时数据消耗用户的空间。