【问题标题】:iPhone: move resources to Application bundleiPhone:将资源移动到应用程序包
【发布时间】:2012-01-02 19:55:58
【问题描述】:

我的申请刚刚被 Apple 拒绝,因为我将资源存储在 Documents 文件夹下! Documents 文件夹会自动同步到 iCloude,因此只有用户生成的数据应该存储在 Documents 下。所有应用程序数据都应该放在应用程序包下。

我在整个项目中使用以下方法

- (NSString *)filePath:(NSString *)fileName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
    return path;
}   

例如,以下行将我的资源解压缩到文档文件夹下。

[SSZipArchive unzipFileAtPath: zipFileName toDestination:[self filePath:@""]];

如何将这些文件和资源文件夹图像移动到应用程序包中并访问它们?

[编辑]

- (NSString *)filePath:(NSString *)fileName {
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/DoNotBackUp"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
        // Set do not backup attribute to whole folder
        if (iOS5) {
            BOOL success = [self addSkipBackupAttributeToItemAtURL:path];
            if (success) 
                NSLog(@"Marked %@", path);
            else
                NSLog(@"Can't marked %@", path);
        }
    }
    path = [path stringByAppendingPathComponent:fileName];

    return path;
}

/*
 set the document files attribute to marked "do not backup"
*/
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSString *)path
{
    const char* filePath = [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;
}

其中 iOS5 为 BOOL 变量:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

iOS5 = NO;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0.1")) 
    iOS5 = YES;

谢谢。

【问题讨论】:

    标签: iphone xcode nsdocumentdirectory app-bundle


    【解决方案1】:

    在 iOS 5.0.1 中你可以添加一个新的文件属性来将它们标记为非 iCloud 同步:

    #include <sys/xattr.h>
    - (void) AddSkipBackupAttributeToFile: (NSURL*) url
    {
        u_int8_t b = 1;
        setxattr([[url path] fileSystemRepresentation], "com.apple.MobileBackup", &b, 1, 0, 0);
    }
    

    【讨论】:

      【解决方案2】:

      您不能在运行时写入 Bundle。您只能在创建二进制文件时将资源添加到包中。您需要将解压缩过程的结果放入缓存目录。

      因为这个文件夹的内容可以随时被系统删除,所以每次启动时都要检查文件是否需要重新解压。

      您可能只需将上述方法的第一行更改为:

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

      【讨论】:

      • OMG 那么我将如何从网站支持我的自动 CMS 内容?如果我从网站更新我的应用程序,我的 iPhone 应用程序会自动下载它并更新文档文件夹。如果整个应用程序 zip 在本地无法作为其巨大文件使用,我将无法下载整个应用程序 zip!一旦本地文件更新,我想确保它是永久性的,因为它会更新版本并与服务器同步,以便服务器知道 iPhone 拥有的文件版本并在需要时发送更改。如果文件被删除,那么我的应用程序可能会进入不一致状态!
      • 在这种情况下,您需要将其标记为永久。 iOS 5.0.1 为此添加了文件系统属性。
      • 嗨 Felix,基于 developer.apple.com/library/ios/#qa/qa1719/_index.html 文档,我不应该为 Documents 文件夹下的文件标记“不备份”。所以我必须按照你在这里的建议去做。苹果还提到缓存文件可能会在存储空间不足的情况下被删除。我找不到如何使我的文件永久化。能给我举个例子吗?
      • 离线数据 这是可以下载或以其他方式重新创建的数据,但用户希望在离线时可靠可用。离线数据应放在 /Documents 目录或 /Library/Private Documents 目录中(详见 QA1699)并标有“不备份”属性。存储在任一位置的数据将在存储空间不足的情况下持续存在,并且“不备份”属性将阻止 iTunes 或 iCloud 备份数据。离线数据文件应在不再存在时立即删除...
      • 看来您需要将文件保留在 Document 中,但在“无备份”的属性中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 2013-04-12
      • 2011-06-03
      • 1970-01-01
      相关资源
      最近更新 更多