2012-07-19 148 views
0

我似乎无法从我的代码中获取备份属性参数。不管我看起来如何做,该方法返回false与“未知的错误-1”。我曾尝试跳过备份方法的不同迭代,但下面是我使用的是最新的基础上,另一篇文章,我发现:Obj-C - 添加'skip-backup'属性总是失败

- (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-E79997F0EC60/Documents/myImage.png

在我的应用程序,它通过循环的所有(众多)文件,我推送到那里,并且它添加了这个属性。它在每一个都失败了。由于这些动态驱动文件对离线功能的要求很高,我需要能够实现这一目标。我没有看到很多其他人都有这个问题,所以这告诉我我可能会错过这里明显的东西,哈哈。

我发现这个职位:Why was my application still rejected after excluding files from iCloud backup using this code?

这看起来很有希望,什么我的问题可能是,但目前还没有真正关心如何具体解决任何细节(它只是建议使用/库,而不是文档的夹?)。

感谢任何人提前谁可以帮助! -Vincent

+0

什么是您的iOS SDK版本和iOS目标版本? – 2012-07-19 16:33:14

+0

对不起,我应该提到这一点。 iOS SDK是:5.1,iOS目标版本是:4.3(但如果需要,可以提出这个问题)。不过,我的设备是最新的操作系统,这就是我从Xcode运行应用程序的地方。谢谢! – svguerin3 2012-07-19 16:37:50

回答

0

为了解决这个问题,我最终只需要在/ 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! :("); 
} 

而当我这样做,它实际上陷入了“没有添加属性”日志语句没有问题!我不知道为什么这在单个文件级别上不起作用,但至少这是有效的,它实际上是一个更好的解决方案,因为现在我不必循环数百个文件来添加属性,而且我只能必须将其添加到一个地方。