3

ダウンロードしたファイルを 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];

助けが必要

4

1 に答える 1

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");
        }
    }
}
于 2012-08-08T08:42:42.050 に答える