【问题标题】:bundleForClass not returning framework bundle iOSbundleForClass 不返回框架包 iOS
【发布时间】:2017-12-14 07:09:47
【问题描述】:

使用 bundleForClass 来检索我定义了一个类的框架包。

NSBundle *bundle = [NSBundle **bundleForClass**: [self class]]; // Class in framework

NSString *readtext = [bundle **pathForResource**:@"Test" ofType:@"rtf"];

获取应用程序的 appbundle(我在哪里使用我的框架)而不是框架包。

如何获取框架包路径并读取框架资源。

【问题讨论】:

  • 根据 NSBundle 的 Apple 文档返回值 - 动态加载 aClass 的 NSBundle 对象(可加载的包),定义 aClass 的框架的 NSBundle 对象,或者如果是 aClass,则为主包对象未动态加载或未在框架中定义。

标签: ios objective-c nsbundle


【解决方案1】:

您可以使用框架的 Bundle Identifier 加载 NSBundle:

[NSBundle bundleWithIdentifier:YOUR_FRAMEWORK_BUNDLE_IDENTIFIRE];

如果框架中有资源包,那么您可以通过以下方式访问资源:

NSBundle *bundle = [NSBundle bundleForClass:[YOUR_CLASS_NAME class]];
NSURL *url = [bundle URLForResource:RESOURCE_BUNDLE_NAME withExtension:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithURL:url];
UIImage* infoImage = [UIImage imageWithContentsOfFile:[resourceBundle pathForResource:@"info" ofType:@"png"]];

【讨论】:

  • 我们可以动态编辑框架资源中的txt文件吗?
  • 您无法编辑该文件,但可以复制到该文件的文档目录并在其中进行更改。
猜你喜欢
  • 2023-03-09
  • 1970-01-01
  • 2019-10-26
  • 2017-08-21
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
相关资源
最近更新 更多