【问题标题】:which URL is given to for setting "Do not backup flag" in ios?哪个 URL 用于在 ios 中设置“不备份标志”?
【发布时间】:2012-08-05 08:00:31
【问题描述】:

我将一些下载的文件存储在 NSDocumentDirectory 中,我需要将这些文件标记为“不备份”。

这是我给出的代码

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL1
    {
       if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1
       const char* filePath = [[URL1 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;
               [URL1 setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];

               return error == nil;
                NSLog(@"success");
            }
      }

对于应该为该方法提供哪个 url 有点困惑?下载 url 或下载的文件路径 url? 我可以像这样使用吗

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);


  NSString *documentsDirectoryPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"MdMus.mp3"];
  [receivedData writeToFile:documentsDirectoryPath atomically:YES];

需要用这个

     NSURL *url=[NSURL fileURLWithPath:documentsDirectoryPath];  
   [self addSkipBackupAttributeToItemAtURL:url];

或者这个

     [self addSkipBackupAttributeToItemAtURL:DownloadingURL];

需要帮助

【问题讨论】:

  • 它应该是第一个url,因为它声明文件不会通过iCloud从那个文件夹备份。我不能用好的参考来回答这个问题,因为我以前没有使用过跳过备份。所以把它写成评论。但我很确定这是从 documentdirectory 获得的 url。
  • definetly 它应该是 url,因为它声明检查 this
  • @iNoob 谢谢,让我检查一下

标签: iphone nsurlconnection nsdocumentdirectory


【解决方案1】:

如果您希望文件共享所有用户数据(在 DocumentDirectory 中的所有数据)阻止备份。参考以下代码。

- (void)addAttributeToAllFolder
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];

    for (int i =0; i < [dirContents count]; i++) {
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",documentsPath,[dirContents objectAtIndex:i]]];
        //this is your method (addSkipBackupAttributeToItemAtURL:)
        if ([self addSkipBackupAttributeToItemAtURL:url]) {
            NSLog(@"success! could add do not backup attribute to folder");
        }
    }
}

【讨论】:

  • 是的。最后,请使用 NSLog 检查控制台。您的表单的 URL 将如下所示。 (/var/mobile/Applications/AB36DF05-A13C-4DC4-8F29-E854050410C8/Documents/blahblah.mp3)
猜你喜欢
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 2022-01-14
  • 1970-01-01
  • 2018-08-16
相关资源
最近更新 更多