【问题标题】:do not Backup flag for iOS 5.1?iOS 5.1 没有备份标志?
【发布时间】:2012-08-05 05:55:54
【问题描述】:

由于数据存储指南中的一些问题,我的第一个音乐下载应用程序被拒绝。我试图将我的 mp3 文件保存在 NSDocumentDirectory 中。我需要通过添加标记“不备份”来重新提交我的应用程序。

这是我的代码,

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



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



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

此代码是否适用于我的 5.1 版本?有点迷茫

【问题讨论】:

    标签: iphone nsurlconnection icloud nsdocumentdirectory


    【解决方案1】:

    参考以下代码。

    How do I prevent files from being backed up to iCloud and iTunes?

    iOS 5.1 and later

    - (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;
    }
    

    in iOS 5.0.1

    #import <sys/xattr.h>
    - (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
    {
        assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
    
        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;
    }
    
    猜你喜欢
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2012-08-05
    • 2018-07-01
    • 1970-01-01
    • 2019-05-04
    • 2012-05-16
    • 2018-08-16
    相关资源
    最近更新 更多