【问题标题】:Can I add the Do not Back up for the "Document Directory" for iCloud我可以为 iCloud 的“文档目录”添加不备份吗
【发布时间】:2013-04-25 08:56:25
【问题描述】:

我读过我可以在 ios 5.1 及更高版本中使用“不备份”属性标记文件夹

据我了解,在这种情况下,目录的所有内容都将从备份中排除。

在我的应用程序中,我们需要从备份中排除 Documents 目录中的所有文件(这些文件可以在应用程序执行期间从 Documents 中添加或删除)。我需要将我们的文件存储在 Documents 目录中。

我可以用“不备份属性”标记文档目录吗?

Apple 允许这样做吗?

这会成为拒绝我们应用的理由吗?

【问题讨论】:

标签: iphone ios objective-c xcode


【解决方案1】:

是的,您可以为 Document 目录的文件夹(文件)设置 do not backup 标志。

do not backup 属性适用于标记的文件,无论它们位于哪个目录,包括 Documents 目录。这些文件不会被清除,也不会包含在用户的iCloud 备份中。由于这些文件确实使用了设备上的存储空间,因此您的应用负责定期监控和清除这些文件。

for more information for the same go through this link

以下方法设置不备份标志以避免不必要的备份|从应用程序目录(文档和缓存)中删除。只需调用以下方法并传递文件夹(文件)的 url。

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL {
    if (&NSURLIsExcludedFromBackupKey == nil) { // for 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 { // For iOS >= 5.1
        NSError *error = nil;
        [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
        return error == nil;
    }
}

【讨论】:

  • 您对 Document 目录的引用告诉我,您可以不备份 Documents 目录中的文件,但不能备份整个 Documents 文件夹本身。无论如何我的解释。
  • @DavidH 是的,你说得对,我认为你应该在 Document 目录中创建一个单独的文件夹,以使其成为整个内容 do not backup
【解决方案2】:

首先,在 Apple 内部论坛上发布此内容,看看是否可以让 Apple 工程师做出回应,我对此表示怀疑。问题是即使它今天有效,它也可能在以后中断。

建议:

1) 在 Documents 中创建一个文件夹,标记它,然后只在其中存储文件。

2) 向您的应用程序委托添加一个方法,该方法采用文件名参数,然后在调用时定位文件并标记它。在这种情况下,您需要确保在每次文件创建操作后调用它。

【讨论】:

    【解决方案3】:

    从 Apple 文档中,对于 iOS 5.1 或更高版本,您应该使用这个:

    - (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
    {
        assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
    
        NSError *error = nil;
        BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                      forKey: NSURLIsExcludedFromBackupKey error: &error];
        if(!success){
            NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
        }
        return success;
    }
    

    【讨论】:

      【解决方案4】:
          func addSkipBackupAttributeToItemAtURL(filePath:String) -> Bool
      
      {
          let URL:NSURL = NSURL.fileURLWithPath(filePath)
          assert(NSFileManager.defaultManager().fileExistsAtPath(filePath), "File \(filePath) does not exist")
          var success: Bool
          do {
              try URL.setResourceValue(true, forKey:NSURLIsExcludedFromBackupKey)
              success = true
          } catch let error as NSError {
              success = false
              print("Error excluding \(URL.lastPathComponent) from backup \(error)");
          }
          return success
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-07
        • 1970-01-01
        • 2013-06-24
        • 1970-01-01
        • 2013-04-17
        相关资源
        最近更新 更多