如何访问 iPhone 应用程序内部的文件?

发布于 2024-08-30 11:37:09 字数 482 浏览 0 评论 0原文

我需要在 iPhone 应用程序中访问构建该应用程序所用的文件(.plist 等)。有一种硬编码的方法可以做到这一点:

NSString *appDir = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
                      objectAtIndex:0] 
                     stringByDeletingLastPathComponent]
                    stringByAppendingPathComponent:appFolder];

其中 appFolder 是文件夹应用程序的名称,例如“test.app”。知道 appDir 后,访问文件就很简单了。

是否有其他非硬编码的方式来访问应用程序中的文件?

提前致谢!

I need in a iPhone app to access files that the app is build with(.plist etc). There's an hardcoded way to do this:

NSString *appDir = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
                      objectAtIndex:0] 
                     stringByDeletingLastPathComponent]
                    stringByAppendingPathComponent:appFolder];

where appFolder is the name of folder app, like "test.app". After the appDir is known, to access files is simple.

Is there any other, not-hardcoded way to have access to files form the app?

Thanks in Advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2024-09-06 11:37:09
 NSString* pathToFile = 
    [[[NSBundle mainBundle] resourcePath] 
         stringByAppendingPathComponent:@"myFile.plist"];

// or, even better (handling localization):
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:@"myFile"
                                                       ofType:@"plist"];
 NSString* pathToFile = 
    [[[NSBundle mainBundle] resourcePath] 
         stringByAppendingPathComponent:@"myFile.plist"];

// or, even better (handling localization):
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:@"myFile"
                                                       ofType:@"plist"];
π浅易 2024-09-06 11:37:09

您的应用程序文件夹是“主包”。所以你可以使用 NSBundle 方法,例如

NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];

Your app folder is the "main bundle". So you can use NSBundle methods such as

NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文