【问题标题】:How should I prevent files from being backed up to iCloud and iTunes on iOS 5.0?在 iOS 5.0 上,我应该如何防止文件被备份到 iCloud 和 iTunes?
【发布时间】:2012-10-09 21:18:07
【问题描述】:

我的应用需要从服务器下载大量内容才能正常运行。

Apple 关于数据存储的指导方针提到,这种“需要工作但易于重新获取”的数据不应包含在 iCloud/iTunes 备份中:这很公平。

棘手的部分是阻止备份目录的代码在 iOS 5.0、5.0.1 和 5.1 之间是不同的(参见this technical note)。

我的应用目前支持 iOS 5.0 作为部署目标。

在以下不同选项之间我应该怎么做:

  • 将部署目标设置为 5.1(直截了当,但我找不到有关仍在使用 iOS 5.0 和 5.0.1 的用户比例的数据,以便向我的老板介绍该主题)
  • 同时实现 Apple 提供的 5.0.1 和 5.1 代码,但这会引发一些问题:
    • 我常用的检测设备是否运行特定 iOS 版本的方法是使用 respondsToSelector: 在我的目标 iOS 版本中引入了一个选择器,但 iOS 5.1 seems to introduce constants and not-universal classes only。如何确定我运行的是 iOS 5.1 或更高版本?
    • 运行 iOS 5.0 的设备会怎样?将数据存储到缓存中对于开发团队和用户体验来说都非常烦人

还有什么可以推荐的吗?

【问题讨论】:

标签: ios ios5 backup itunes icloud


【解决方案1】:

对于存储到文件系统的每个文件,您必须添加“不备份”属性。请参阅此答案:Adding the "Do Not Backup" attribute to a folder hierarchy in iOS 5.0.1。要检查系统版本,您可以调用[[UIDevice currentDevice] systemVersion],或使用__IPHONE_5_0 等宏与#if defined()#ifndef。祝你好运!

【讨论】:

  • 这就是我之前所做的,但现在我在 iOS 6 设备上运行,setxattr 方法不再返回 0。所以这种方法已经不够用了。
  • 感谢 __IPHONE_5_0 提示! (就 systemVersion 而言,我认为这是获取 iOS 版本的最直接方式,但我不喜欢它,因为它需要一些 NSString 解析以及对即将推出的 iOS 版本名称的一些期望)
【解决方案2】:

我正在将我的图像文件夹标记为不备份,因为我正在下载图像以供离线使用。 Hereapple 讨论如何做到这一点。

#include<sys/xattr.h>

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL {
    if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1
        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;
    } else { // iOS >= 5.1
        NSError *error = nil;
        [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
        return error == nil;
    }
}

- (void)createSkipBackupImagesFolder {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/images"];

    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
        NSURL *toURL = [NSURL fileURLWithPath:dataPath];
        [self addSkipBackupAttributeToItemAtURL:toURL];
    }
}

[self createSkipBackupImagesFolder];

【讨论】:

    【解决方案3】:

    如果您将数据存储到缓存目录而不是文档目录中,那么 icloud 将不会备份您的数据...

    所以我认为这是停止 icloud 备份的最佳方式。

    在“不备份”属性中,您需要为每个文件设置此标志。

    使用 NSCachesDirectory 安装 NSDocumentDirectory。

    【讨论】:

    • 文件可以随时从缓存目录中删除,并且可以将“不备份”属性设置为文件夹,这意味着该文件夹中的文件也不会被备份。如果您要帮助人们,请确保您的帮助至少能帮助他们 70%,就像苹果一样 :)
    • 您好 fahri 感谢您投反对票...但我的一个应用程序因 icloud 备份问题而被苹果拒绝。之后我搜索了很多并找到了缓存目录的解决方案...
    • 然后在我将 3 个应用程序上传到商店后,它与缓存目录完美配合...不会从此目录中删除任何数据...
    • @DirtyHenry 我只是想帮助你。如果您喜欢我的解决方案,那么请不要使用此解决方案...但是我的解决方案是完美的。我的公司正在将其用于所有应用程序...祝您好运。
    猜你喜欢
    • 2013-11-03
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2012-09-04
    • 2014-05-04
    相关资源
    最近更新 更多