NSURLIsExcludedFromBackupKey - 应用必须遵循iOS数据存储指南,否则将被拒绝

时间:2012-06-26 09:59:50

标签: ios iphone icloud appstore-approval

我的应用被拒绝,因为似乎7 MB存储在文档文件夹中,并且它们会自动发送到icloud。所以我已经通过这种方法循环将写入文档文件夹的所有文件:

- (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
   NSError *error = nil;
   [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
   return error == nil;
  }

我的应用程序版本1.1在此代码实现后获得批准。上周我试图发送相同应用程序的1.2版本(文件管理中没有任何变化,存储在文档文件夹中的所有文件都通过addSkipBackupAttributeToItemAtURL方法循环)。出于同样的原因,我的应用再次遭到拒绝。我无法将我的文件移动到临时文件夹或缓存文件夹,因为我的应用程序无法完全恢复文件(其中一个文件是数据库,恢复数据库意味着任何用户插入的数据都松散),所以这一个不能解决。无论如何,我在代码中发现了一个问题,这就是我称之为方法的方法:

[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:fullPath]];

使用[NSURL fileURLWithPath:fullPath]设备和ios 5.1返回错误,似乎无法创建属性。如果我用[NSURL URLWithString:defaultStorePath]更改nsurl的初始化,带5.1的设备似乎正确添加了该属性。

对于ios 5.0.1,所有内容都被反转,[NSURL URLWithString:defaultStorePath]在[NSURL fileURLWithPath:fullPath]工作时返回错误。

也许我可以检查ios版本并设置适当的nsurl初始化,但它仍然是一个问题。在拒绝解释中我读到:

特别是,我们发现在启动和/或内容下载时,您的应用会存储7mb。要检查应用存储的数据量:

  • 安装并启动您的应用
  • 转到设置> iCloud>存储&备份>管理存储
  • 如有必要,请点按“显示所有应用”
  • 检查应用的存储空间

如果我尝试检查这个值,我看到7 mb也正确的nsurl初始化(当所有属性设置正确时)。什么是正确的行为?谁有这个问题?在苹果建议的应用程序存储检查之前,我是否必须做一些特定的事情才能使其显着?

2 个答案:

答案 0 :(得分:2)

我认为诀窍是添加NSURLIsExcludedFromBackupKey并确保该目录位于文档目录之外。我这样做是通过将我的文档移动到Library / Application Support文件夹(因为它在/ tmp或/ Caches文件夹中没有意义):

// store in /Library/Application Support/BUNDLE_IDENTIFIER/Reference
// make sure Application Support folder exists
NSURL *applicationSupportDirectory = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
                                                                            inDomain:NSUserDomainMask
                                                                   appropriateForURL:nil
                                                                              create:YES
                                                                               error:&error];
if (error) {
    NSLog(@"KCDM: Could not create application support directory. %@", error);
    return nil;
}

NSURL *referenceFolder = [applicationSupportDirectory URLByAppendingPathComponent:@"Reference" isDirectory:YES];
if (![[NSFileManager defaultManager] createDirectoryAtPath:[referenceFolder path]
                               withIntermediateDirectories:YES
                                                attributes:nil
                                                     error:&error]) {
    NSLog(@"KCDM: Error creating Reference folder to store model %@: %@", modelName, error);
    return nil;
}

BOOL success = [referenceFolder setResourceValue:@YES forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
    NSLog(@"KCDM: Error excluding %@ from backup %@", referenceFolder, error);
}

答案 1 :(得分:0)

在我从设备上删除我的应用程序并重新安装之前,我遇到了同样的问题。我还必须通过转到设置 - >存储和备份 - >来删除iCloud备份中的现有缓存数据。管理存储

这似乎可以解决问题。 另外,我添加skip属性的代码有点不同: Code lifted from this post

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
   assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

   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
      NSError *error = nil;
      [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
      return error == nil;
   }


}