iPhone:可以访问模拟器中文档目录中的文件,但不能访问设备 [英] iPhone: Can access files in documents directory in Simulator, but not device

查看:66
本文介绍了iPhone:可以访问模拟器中文档目录中的文件,但不能访问设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing an app that copies some contents of the bundle into the applications Document's directory, mainly images and media. I then access this media throughout the app from the Document's directory.

这在Simulator中完全可以正常工作,但在设备上则无法正常工作.资产仅显示为空.我已经完成了NSLog,文件的路径看起来正确,并且通过在控制台中转储文件列表来确认目录中存在文件.

This works totally fine in the Simulator, but not on the device. The assets just come up as null. I've done NSLog's and the paths to the files look correct, and I've confirmed that the files exist in the directory by dumping a file listing in the console.

有什么想法吗?谢谢!

编辑

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

Here's the code that copies to the Document's directory

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目录的路径:

I am figuring out the path to the docs directory with this method:

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

}

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

And here's an example of how I'm building a path to an image

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

推荐答案

原来是一个问题,其中捆绑包未在设备上更新,并且显然没有与Simulator相同的文件集.这篇博客文章有所帮助: http://majicjungle.com/blog/?p=123

It turned out to be an issue where the bundle was not being updated on the device and apparently didn't have the same set of files that the Simulator had. This blog post helped a bit: http://majicjungle.com/blog/?p=123

基本上,我清理了构建并删除了该应用程序,然后直接将其直接安装到设备上,而不是直接安装到模拟器上.有趣的东西.

Basically, I cleaned the build and delete the app and installed directly to the device first instead of the simulator. Interesting stuff.

这篇关于iPhone:可以访问模拟器中文档目录中的文件,但不能访问设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆