【问题标题】:iPhone: Can access files in documents directory in Simulator, but not deviceiPhone:可以访问模拟器中文档目录中的文件,但不能访问设备
【发布时间】:2011-02-01 08:29:57
【问题描述】:

我正在编写一个应用程序,它将捆绑包的一些内容复制到应用程序文档的目录中,主要是图像和媒体。然后我从 Document 的目录访问整个应用程序中的这个媒体。

这在模拟器中完全可以正常工作,但在设备上却不行。资产只是出现为空。我已经完成了 NSLog 并且文件的路径看起来是正确的,并且我通过在控制台中转储文件列表来确认文件存在于目录中。

有什么想法吗?谢谢!

编辑

这是复制到文档目录的代码

NSString *pathToPublicationDirectory = [NSString stringWithFormat:@"install/%d",[[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]];
    NSString *manifestPath = [[NSBundle mainBundle] pathForResource:@"content" ofType:@"xml" inDirectory:pathToPublicationDirectory];
    [self parsePublicationAt:manifestPath];

    // Get actual bundle path to publication folder
    NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathToPublicationDirectory];
    // Then build the destination path
    NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", [[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]]];

    NSError *error = nil;
    // If it already exists in the documents directory, delete it
    if ([fileManager fileExistsAtPath:destinationPath]) {
        [fileManager removeItemAtPath:destinationPath error:&error];
    }
    // Copy publication folder to documents directory
    [fileManager copyItemAtPath:bundlePath toPath:destinationPath error:&error];

我正在用这种方法找出 docs 目录的路径:

- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

}

这是我如何构建图像路径的示例

path = [NSString stringWithFormat:@"%@/%d/%@", [self applicationDocumentsDirectory], [[thisItem valueForKey:@"publicationID"] intValue], [thisItem valueForKey:@"coverImage"]];

【问题讨论】:

  • 根据我的经验,模拟器的访问限制较少。我相信我遇到过模拟器让我直接写入包的情况。一些代码将有助于了解发生了什么。

标签: iphone iphone-sdk-3.0 ios-simulator


【解决方案1】:

事实证明,捆绑包没有在设备上更新,并且显然没有模拟器所拥有的相同文件集。这篇博文有点帮助:http://majicjungle.com/blog/?p=123

基本上,我清理了构建并删除了应用程序并首先直接安装到设备而不是模拟器。有趣的东西。

【讨论】:

    【解决方案2】:

    我看不到 documentsDirectory 的定义位置。

    NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", [[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]]];
    

    也许以下方法可以解决问题

    NSString *destinationPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", [[[manifest objectAtIndex:i] valueForKey:@"publicationID"] intValue]]];
    

    【讨论】:

    • 前面已经定义过了,抱歉我漏掉了那部分。 NSString *documentsDirectory = [self applicationDocumentsDirectory];如果没有定义,应用程序可能会崩溃。
    • parsePublicationAt: 是做什么的?它是在单独的线程上解析吗?在设备上,它可能没有完成。在文件管理器操作后检查错误将很有帮助。 NSLog(@"fileManager error: %@", [error localizedDescription]);
    • 它在指定的位置解析一些 XML。我查看了文件管理器是否产生错误,但它返回 true。我可以看到文件在文档目录中,所以看起来它们被复制过来就好了。谢谢!
    【解决方案3】:

    您的复制代码看起来不错,并且您说您没有收到任何错误。

    但我对“资产只是空值”很感兴趣。您确定稍后使用确切相同的名称访问文件名吗?

    与模拟器不同,真正的 iPhone 具有区分大小写的文件系统。

    【讨论】:

    • 啊,这很有趣,很高兴知道。当我们直接从包中访问时,它对我们有用,所以我们的文件名是正确的。不过谢谢!
    猜你喜欢
    • 2011-07-26
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多