- 关键数据应该存放在 <Application_Home>/Documents目录下,这里的关键数据是指那些应用不能重新生成的数据,比如用户文档或者用户生成的其它数据;
- 支持文件指的是那些由应用下载或者生成,并且在需要的时候能够重新创建的文件,这类文件的存储位置取决于当前使用的系统版本。
- 在iOS5.1及其之后的版本中,支持文件应存放在<Application_Home>/Library/Application Support目录下,此外,还需使用setResourceValue:forKey:error:方法给相应文件的NSURL对象添加NSURLIsExcludedFromBackupKey属性(如果应用使用的是Core Fundation库,那么使用CFURLSetResourcePropertyForKey函数给文件对应的CFURLRef对象添加kCFURLIsExcludedFromBackupKey值),添加此属性是为了防止这类文件被备份到iTunes或者iCloud,如果存在大量的支持文件,为了方便起见,可以创建一个子目录来存放所有的支持文件,这样只需给子目录设置无需备份的属性即可;
- 在iOS5.0.1版本中,苹果对这个版本支持文件存放的目录没做强制的规定,但是与5.1版本一样需要给文件添加无需备份的属性,设置方法和iOS5.1不同,具体见本文最后链接。
- 在iOS5.0及其之前的版本中,支持文件应当存放到<Application_Home>/Library/Caches目录下。
- 缓存数据存放在<Application_Home>/Library/Caches目录下,这类文件包括(但是不限于)数据库缓存文件以及下载内容,就如杂志,报纸,地图类应用中使用的一样。应用本身需要能够处理当磁盘空间不足导致系统删除这类缓存文件的情况。
- 临时数据存放在 <Application_Home>/tmp目录下,临时数据指的是那些不需要在很长一段时间内存在的文件,为了节省用户的设备空间,应用应当在不需要这类数据的时候将其即时删除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
- (
BOOL
)addSkipBackupAttributeToFileAtPath:(
NSString
*)aFilePath
{
assert([[
NSFileManager
defaultManager] fileExistsAtPath:aFilePath]);
NSError
*error =
nil
;
BOOL
success =
NO
;
NSString
*systemVersion = [[UIDevice currentDevice] systemVersion];
if
([systemVersion floatValue] >= 5.1f)
{
success = [[
NSURL
fileURLWithPath:aFilePath] setResourceValue:[
NSNumber
numberWithBool:
YES
]
forKey:
NSURLIsExcludedFromBackupKey
error:&error];
}
else
if
([systemVersion isEqualToString:
@"5.0.1"
])
{
const
char
* filePath = [aFilePath fileSystemRepresentation];
const
char
* attrName =
"com.apple.MobileBackup"
;
u_int8_t attrValue = 1;
int
result = setxattr(filePath, attrName, &attrValue,
sizeof
(attrValue), 0, 0);
success = (result == 0);
}
else
{
NSLog
(
@"Can not add 'do no back up' attribute at systems before 5.0.1"
);
}
if
(!success)
{
NSLog
(
@"Error excluding %@ from backup %@"
, [aFilePath lastPathComponent], error);
}
return
success;
}
|
参考链接: http://www.yebw.me/?p=6