do not Backup flag for iOS 5.1?

ⅰ亾dé卋堺 提交于 2019-12-10 23:05:02

问题


my first music downloading app get rejected due to some issues in data storage guidelines. i tried to save my mp3 file in NSDocumentDirectory. i need to resubmit my app by adding flag "Do not backup".

here is my code,

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

will this code work fine for my 5.1 version? bit confused


回答1:


refer a following code.

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


来源:https://stackoverflow.com/questions/11859166/do-not-backup-flag-for-ios-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!