【问题标题】:Can't access resource bundle from custom framework using CoreFoundation无法使用 CoreFoundation 从自定义框架访问资源包
【发布时间】:2014-12-31 18:22:38
【问题描述】:

我做了什么

我已经构建了一个自定义的 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

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

谢谢

【问题讨论】:

    标签: ios frameworks bundle core-foundation


    【解决方案1】:

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

    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);
    

    【讨论】:

      猜你喜欢
      • 2014-11-07
      • 1970-01-01
      • 2016-05-05
      • 2018-06-19
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多