framework资源文件读取

1.在framework里面读framwork自己的资源文件

这是framework内部的资源,跟其他都没有关系。但是framework不能单独存在,必须要放在某个“主程序”中才能起作用。bundle参数如果不传,那么默认是mainBundle,这种情况路径就不对了。这种情况下,可以用下面这个API来获得bundle参数。

  // 获取bundle参数

    NSBundle *bundle = [NSBundle bundleForClass:self.class];

    // 读UIStoryboard

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@“StoryboardName” bundle:bundle];

    // 读UIImage

    UIImage *image = [UIImage imageNamed:@"icon_back_gray" inBundle:bundle compatibleWithTraitCollection:nil];

    // 文件路径

    NSString* htmlPath = [bundle pathForResource:@"index" ofType:@"html"];

2.在主程序中读framework里面的资源文件

同样也是利用bundle参数来读取,class选择framework中某个导出的class就可以了。

// 获取bundle参数,ZAFinanceFrameworkManager是framework中接口类

    NSBundle *bundle = [NSBundle bundleForClass:[ZAFinanceFrameworkManager class]];

3.在framework中读主程序的资源文件

这个和在主程序中读自己的一样,不需要bundle参数,一定要传的话,就传nil或者[NSBundle mainBundle]

4.从framework里面读其他framwork里面的资源文件

同样也是利用bundle参数来读取,class选择目标framework中某个导出的class就可以了。

小结

在单体程序中,NSBundle这个参数不需要管,全部传nil或者是默认的[NSBundle mainBundle]就可以了。

引入了framework之后,就需要NSBundle这个参数来区分资源所在的模块。确定NSBundle比较简单的方法是用下面这个API,其中的class只要选择资源所在的framework中的某个class就可以了。获取到bundle后,通过bundle获取资源文件用法一致。

+ (NSBundle *)bundleForClass:(Class)aClass;
原文地址:https://www.cnblogs.com/liuluoxing/p/6594015.html