OGeek|极客世界-中国程序员成长平台

标题: ios - 无法使用 CoreFoundation 从自定义框架访问资源包 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:12
标题: ios - 无法使用 CoreFoundation 从自定义框架访问资源包

我做了什么

我已经构建了一个自定义的 iOS 框架,我们将称之为 CustomFramework.framework 以及一个用于一些名为 CustomFramework.bundle 的 sqlite 文件的随附资源包。我已将我的框架和资源包添加到另一个项目中,并且构建成功。

我要做什么

我正在尝试使用 CoreFoundation 访问资源包。然后我想选择一个特定的 sqlite 文件并打开它。

出了什么问题

我的框架有一个名为ResourceHelper.cpp的文件。这是我要打开 sqlite 文件的地方。我正在尝试获取对 CustomFramework.bundle 包的引用。为此,我有这行:

CFBundleRef mainBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.mycompany.CustomFramework"));

在我的 CustomFramework-Info.plist 中,Bundle identifer 字段是 com.mycompany.CustomFramework。这行代码返回NULL;在调试器中它看起来像 mainBundle = (CFBundleRef) 0x0.

为什么我找不到我的框架资源包?

谢谢



Best Answer-推荐答案


最终的工作是我必须将包名称附加到主包路径。这是它的工作原理。

std::string dbName = "database";
std::string resourcePath = "CustomFramework.bundle/";

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFStringRef cf_dbName = CFStringCreateWithCString(NULL, dbName.c_str(), kCFStringEncodingUTF8);
CFStringRef cf_path = CFStringCreateWithCString(NULL, resourcePath.c_str(), kCFStringEncodingUTF8);

CFURLRef url = CFBundleCopyResourceURL(mainBundle, cf_dbName, CFSTR("sqlite"), cf_path);

关于ios - 无法使用 CoreFoundation 从自定义框架访问资源包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26742304/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4