我似乎无法从代码中获得备份属性参数。无论我看起来做了什么,该方法都会返回false,并显示"未知错误-1"。我尝试过不同的跳过备份方法迭代,但以下是我使用的最新方法,基于我发现的另一篇文章:
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
if (&NSURLIsExcludedFromBackupKey == nil) {
// iOS 5.0.1 and lower
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
} else {
// First try and remove the extended attribute if it is present
int result = getxattr(filePath, attrName, NULL, sizeof(u_int8_t), 0, 0);
if (result != -1) {
// The attribute exists, we need to remove it
int removeResult = removexattr(filePath, attrName, 0);
if (removeResult == 0) {
NSLog(@"Removed extended attribute on file %@", URL);
}
}
// Set the new key
return [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil];
}
}
我对该方法的调用如下(它在运行时总是打印"不成功"):
if ([self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:filePath]]) {
NSLog(@"success!");
} else {
NSLog(@"not successful");
}
filePath是指向文档文件夹图像的NSString路径。我记录了一个图像路径的例子如下:
/var/mobile/Applications/B3583D06-38BC-45F0-A027-E7997F0EC60/Documents/myImage.png
在我的应用程序中,它循环浏览我推送的所有(大量)文件,并添加了这个属性。它在他们每一个人身上都失败了。我需要能够做到这一点,因为这些动态驱动的文件需要离线功能。我没有看到很多其他人有这个问题,所以告诉我可能在这里错过了一些明显的东西,哈哈。
我发现这篇文章:为什么我的应用程序在使用此代码从iCloud备份中排除文件后仍然被拒绝?
关于我的问题可能是什么,这看起来很有希望,但实际上没有任何关于如何具体解决的细节(它只是建议使用/Library而不是docs文件夹?)。
感谢任何能够提前提供帮助的人!-Vincent
为了解决这个问题,我最终只需要在/Documents目录中创建一个文件夹,然后我更改了应用程序,将所有文件指向该子文件夹。
因此,在我的AppDelegate中,在预先加载任何内容之前,我添加了如下代码:
NSString *tempDir = [documentsDirectory stringByAppendingString:@"/tempfiles"];
[[NSFileManager defaultManager] createDirectoryAtPath:tempDir
withIntermediateDirectories:NO
attributes:nil
error:nil];
NSURL *tempDirPathURL = [NSURL URLWithString:tempDir];
if ([[MainControl controlSystem] addSkipBackupAttributeToItemAtURL:tempDirPathURL]) {
NSLog(@"did add attribute!");
} else {
NSLog(@"adding attribute failed! :(");
}
当我这样做的时候,它实际上进入了"添加属性"日志语句,没有任何问题!我不知道为什么这在单个文件级别不起作用,但至少它起作用了,而且它实际上是一个更好的解决方案,因为现在我不必遍历数百个文件来添加属性,而且我只需要在一个地方添加它。