0

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

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

棘手的部分是阻止目录备份的代码在 iOS 5.0、5.0.1 和 5.1 之间是不同的(参见本技术说明)。

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

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

  • 将部署目标设置为 5.1(直截了当,但我找不到有关仍在使用 iOS 5.0 和 5.0.1 的用户比例的数据,以便轻松地将主题介绍给我的老板)
  • 实现 Apple 提供的 5.0.1 和 5.1 代码,但它引发了一些问题:
    • 我通常检测设备是否运行特定 iOS 版本的方法是使用 respondsToSelector: 在我的目标 iOS 版本中引入了选择器,但 iOS 5.1似乎只引入了常量和非通用类。如何确定我运行的是 iOS 5.1 或更高版本?
    • 运行 iOS 5.0 的设备呢?将数据存储到缓存中对于开发团队和用户体验来说都是非常烦人的

还有其他选择推荐吗?

4

3 回答 3

0

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

#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];
于 2015-01-15T06:18:18.463 回答
0

对于存储到文件系统的每个文件,您必须添加“不备份”属性。请参阅此答案:将“不备份”属性添加到 iOS 5.0.1 中的文件夹层次结构。要检查系统版本,您可以调用[[UIDevice currentDevice] systemVersion],或使用__IPHONE_5_0with#if defined()和之类的宏#ifndef。祝你好运!

于 2012-10-19T09:29:05.470 回答
-1

if you store your data into Cache directory instead of document directory than icloud will not backup your data...

so i think this is the best way to stop icloud backup.

in "do not backup" attribute you need to set this flag for each and every file.

use NSCachesDirectory insted NSDocumentDirectory.

于 2012-10-19T09:37:15.750 回答