【问题标题】:Including files inside the iPhone Bundle Library Path在 iPhone Bundle Library Path 中包含文件
【发布时间】:2010-09-12 16:59:51
【问题描述】:

我的更新逻辑有问题。我需要在包含特定更新数据的应用程序包中包含一个文件。当应用程序发现这个文件时,它会检查它是第一次安装(不需要更新)还是以前的安装(应用更新)。不管结果如何,更新文件都需要删除,所以再也找不到了,否则每次运行应用都会应用更新,很糟糕。

由于无法从 [[NSBundle mainBundle] 中删除任何内容,因此我需要找出最好的方法来执行此操作。如果我可以在应用程序的库路径中包含更新文件,那就简单多了。

我需要这个的原因是因为在第一次加载时,应用程序会创建一个用户数据文件并将其存储在库路径中。从那时起,应用程序将加载该用户文件。在这种情况下,创建的文件具有过时的数据。我创建了一个包含更新数据的新文件以应用于用户的主文件。

谁能帮我解决这个问题?这是我所拥有的:

if ([self checkUpdateFile] == YES) {
    [self applyUpdate];
}

-(BOOL)checkUpdateFile {
NSString *updateFilePath = [[NSBundle mainBundle]pathForResource:@"updateData"
        ofType:@"dat" inDirectory:@"update"];
BOOL updateFileExists = [[NSFileManager defaultManager]
        fileExistsAtPath: updateFilePath];
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
        NSUserDomainMask, YES) objectAtIndex:0];
NSString *programDataPath = [libraryPath stringByAppendingPathComponent: 
        @"programData.dat"];
BOOL programFileExists = [[NSFileManager defaultManager]fileExistsAtPath: 
        programDataPath];
if (programFileExists == YES && updateFileExists == YES) {
    NSLog(@"Update File is present, and so is a data file, so this is a previous install");
    return YES;
} else {
    NSLog(@"This is not an upgradeable version.");
    if (updateFileExists == YES) {
        NSLog(@"The update file is here but this is the first install.");
        [[NSFileManager defaultManager]removeItemAtPath: updateFilePath 
                        error:NULL];
        BOOL doesFileStillExist = [[NSFileManager defaultManager] 
                        fileExistsAtPath:updateFilePath];
        if (doesFileStillExist == YES) {
            NSLog(@"File still exists");
        } else {
            NSLog(@"File was deleted.");
        }
    }
    return NO;
}
}

-(void)applyUpdate {
NSLog(@"Applying Update");
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"updateData" 
        ofType:@"dat" inDirectory:@"update"];
NSData *programData = [[NSData alloc] initWithContentsOfFile:filePath];
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] 
        initForReadingWithData:programData];
NSMutableArray *characterList = [[decoder 
        decodeObjectForKey:@"characterList"]retain];
int i = 0;
for (Character *player in characterList) {
    NSMutableArray *movesList = player.moves;
    Character *existingCharacter = [self.dataController.characterList 
                objectAtIndex:i];
    NSLog(@"Found Character: %@",existingCharacter.name);
    existingCharacter.moves = movesList;
    i++;
}

BOOL doesFileStillExist = [[NSFileManager defaultManager] 
         fileExistsAtPath:filePath];
if (doesFileStillExist == YES) {
    NSLog(@"File still exists");
} else {
    NSLog(@"File was deleted.");
}
[self writeDataToDisk];
[characterList release];
[decoder release];
[programData release];

}

【问题讨论】:

    标签: iphone xcode ipad ios4


    【解决方案1】:

    如果应用在首次启动时正在运行,请进行更新。由于更新数据是您的主包的一部分,您将无法删除该文件

    完成更新后,将 BOOL 条目写入 plist 文件(类似于 appIsUpdated)。

    在后续启动时,检查 plist 文件是否存在以及 appIsUpdated 的值。如果 !appIsUpdated,请更新应用程序。

    【讨论】:

      【解决方案2】:

      我发现在将文件添加到主包时,有时在代码的主包中找不到它们。该文件将出现在文件窗格中,但使用以下代码无法识别或找到它:

      NSString *file = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"db"];
      

      选择有问题的文件并删除它的参考,然后使用菜单项: 并将文件添加回项目。这似乎触发了 Xcode 中的一些内部标志,导致编译器将其重新编译到项目中。

      【讨论】:

        【解决方案3】:

        您回答了自己的问题;您不能删除应用程序包中的文件。将整个捆绑包视为只读目录。

        您必须明确说明为什么要这样做,因为我从您的描述中无法理解。尝试在高层次上解释它(“功能”描述)——你想要发生什么,为什么? (不是如何你希望某事发生,这是一个低级或“非功能性”的描述。)

        【讨论】:

        • 用户第一次启动应用程序,并以编程方式创建一个数据文件,然后写入磁盘。在随后的启动中,数据文件会从磁盘重新初始化。随着时间的推移,这些数据会被淘汰,但其中仍有重要的部分。为了维护用户的数据而不是破坏它/从头开始,我想用一些新的数据“附加”用户的数据文件。我选择只在 userDefaults 中创建一个 BOOL,表明已应用更新,并忽略我的 mainBundle 中的更新文件。我宁愿删除 updateFile,但是,好吧。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        • 2013-12-15
        • 2019-02-10
        • 2021-01-29
        • 2018-02-17
        • 2012-02-23
        相关资源
        最近更新 更多