不为iOS 5.1备份标志? [英] do not Backup flag for iOS 5.1?

查看:83
本文介绍了不为iOS 5.1备份标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个音乐下载应用因数据存储准则中的某些问题而被拒绝.我试图将我的mp3文件保存在NSDocumentDirectory中.我需要通过添加标志请勿备份"来重新提交我的应用.

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".

这是我的代码,

  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版本中可以正常工作吗?有点困惑

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

推荐答案

请参考以下代码.

如何防止文件备份到iCloud和iTunes ?

iOS 5.1 and later

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

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

这篇关于不为iOS 5.1备份标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆