用于在ios中设置“不备份标志”的URL?

时间:2012-08-08 08:31:13

标签: iphone nsurlconnection nsdocumentdirectory

我将一些下载的文件存储在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");
            }
      }

littile有点混淆应该为这个方法提供哪个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];

需要帮助

1 个答案:

答案 0 :(得分:2)

如果您希望文件共享所有用户数据(在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");
        }
    }
}