使用NSBundle访问iOS7中的资产 [英] Accessing assets in iOS7 with NSBundle

查看:89
本文介绍了使用NSBundle访问iOS7中的资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将应用程序更新到iOS7 SDK时遇到问题.在使用iOS 6 SDK并使用NSURL来访问我的mp3文件之前,像这样的文件夹:

I am having problems updating an app to iOS7 SDK. Before I've used iOS 6 SDK and accessed my mp3 file using a NSURL for the folder like this:

NSURL *folderURL = [[NSBundle mainBundle] URLForResource:@"" withExtension:@"" subdirectory:@"AudioGuide"];

现在,使用iOS 7 SDK,我总是得到nil作为folderURL的值,而我的音频指南再也找不到mp3了.

Now, using the iOS 7 SDK I always get nil as the value for folderURL and my audio guide doesn't find the mp3's anymore.

我已经查看了为模拟器生成的.app捆绑包(在〜/Library/Application Support/...中),并且我可以在根目录中看到"AudioGuide"文件夹.所以它肯定在那里.

I've already looked into the generated .app-Bundle for the simulator (in ~/Library/Application Support/...), and I can see the "AudioGuide" folder in the root. So it's definitly there.

我不是iOS专家,也没有真正关注iOS7更新.在访问应用程序中自己的资产方面是否进行了任何更改?如何访问我的文件?

I am not that iOS guru and didn't really follow iOS7 updates. Has there been any changes made on how to access own assets in an app? How do I access my files?

推荐答案

- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)extension subdirectory:(NSString *)subpath

返回由指定文件标识的资源文件的文件URL 名称和扩展名,并位于给定的捆绑软件目录中.

Returns the file URL for the resource file identified by the specified name and extension and residing in a given bundle directory.

这意味着您可以在给定的子目录中获取具有特定类型的文件URL,因此您应该使用此方法调用以及文件名和扩展名.

what that means is you can get the file URL with specific type in a given subdirectory, so you should use this method call with your file name and extension.

如果您尝试读取该目录下的文件列表,则可以将所有音频文件捆绑在一个zip或gzip压缩文件中,并在访问文件时将其解压缩到文档"或应用程序支持"目录中第一次,那么您可以使用下面的代码阅读它.

If you are trying to read the list of files under that directory, what you can do is bundle all of your audio files in a single zip or gzipped file and you extract it in your Documents or Application Support directory when it is accessed for the very first time, then you can read it using the code below.

NSArray *directories = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *directory = [directories objectAtIndex:0];
NSString *audioDirectoryPath = [directory stringByAppendingPathComponent:@"AudioGuide"];


NSArray *audioFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:audioDirectoryPath];

希望这会有所帮助!

这篇关于使用NSBundle访问iOS7中的资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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