我想,这样它从iCloud云备份中排除,以纪念我的应用程序的NSDocumentDirectory的整个文件夹,但是当我去到终端,运行:XATTR -plxv com.apple.MobileBackup我得到这个错误:没有这样的XATTR: com.apple.MobileBackup
谢谢你提前提供任何帮助。
下面是我使用的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *pathURL= [NSURL fileURLWithPath:documentsDirectory];
[self addSkipBackupAttributeToItemAtURL:pathURL];
}
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1
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;
} else { // iOS >= 5.1
NSLog(@"%d",[URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil]);
return [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil];
}
}
你怎么能在你的iOS应用程序的文件夹中运行终端? 你的意思是在模拟器?
可以肯定苹果将忽略或从主文档文件夹中去掉该属性。 你应该做的是什么苹果告诉开发人员做的(从文件系统编程指南 ):
手柄支持文件,文件在您的应用程序下载或生成并可以重新创建按需使用以下两种方法之一:
在安装iOS 5.0和更早的版本,把支持文件的<APPLICATION_HOME> /Library/Caches
目录,以防止它们被备份
在iOS系统5.0.1或更高版本,把支持文件的<APPLICATION_HOME> /Library/Application Support
目录和com.apple.MobileBackup扩展属性适用于他们。 此属性防止文件被备份到iTunes或iCloud中。 如果你有大量的支持文件,可以将它们存储在自定义子目录和扩展属性应用到刚刚建立的目录。
所以,你创建你的内部文件的新目录Application Support
,以及属性应用到该目录中。
编辑:嗯,看来的信息是过时的和文件尚未更新。 从iOS的5.1版本说明 :
的iOS 5.1引入了一个新的API标记文件,或不应被备份的目录。 对于NSURL
对象,添加NSURLIsExcludedFromBackupKey
属性,以防止相应的文件被备份。 对于CFURLRef objects
,使用相应的kCFURLIsExcludedFromBackupKey
属性。
应用程序在iOS 5.1上运行以后必须使用新的属性,而不是添加com.apple.MobileBackup
直接扩展属性,如以前记录。 所述com.apple.MobileBackup
扩展属性已被弃用,对它的支持可能在将来的版本中删除。
事实证明,就可以得到实际的代码在做这个技术Q&A QA1719 。
另外,我发现我需要创建应用程序支持目录,至少在模拟器。 希望这有助于代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// got to make sure this exists
NSFileManager *manager = [NSFileManager defaultManager];
NSString *appSupportDir = [self applicationAppSupportDirectory];
if(![manager fileExistsAtPath:appSupportDir]) {
__autoreleasing NSError *error;
BOOL ret = [manager createDirectoryAtPath:appSupportDir withIntermediateDirectories:NO attributes:nil error:&error];
if(!ret) {
LTLog(@"ERROR app support: %@", error);
exit(0);
}
}
...
}
- (NSString *)applicationAppSupportDirectory
{
return [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject];
}