iphone - 在ios中设置 "Do not backup flag"的URL是哪个?

标签 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");
            }
      }

对于该方法应该给出哪个 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];

需要帮助

最佳答案

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

关于iphone - 在ios中设置 "Do not backup flag"的URL是哪个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11860632/

相关文章:

Objective-C SSL同步连接

ios - 在重定向之前停止 NSURL 连接,同时获取重定向 URL

ios - 如果目录不存在,是否有更安全的方法来创建目录?

iPhone 应用程序通讯

iphone - Xcode 4.3.1 将文件导入新项目

iphone - 使用 UILongPressGestureRecognizer 和 UIBarButtonItem

iphone - 将 subview 添加到 UITableViewCell 内容 View

ios - NSURLConnection didReceiveData 未加载数据

ios - 如何将多个视频保存到应用程序文档目录中?

iphone - 从文档目录中检索多个图像