framework資源文件讀取

1.在framework裏面讀framwork本身的資源文件

這是framework內部的資源,跟其餘都沒有關係。可是framework不能單獨存在,必需要放在某個「主程序」中才能起做用。bundle參數若是不傳,那麼默認是mainBundle,這種狀況路徑就不對了。這種狀況下,能夠用下面這個API來得到bundle參數。html

  // 獲取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就能夠了。spa

// 獲取bundle參數,ZAFinanceFrameworkManager是framework中接口類

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

3.在framework中讀主程序的資源文件

這個和在主程序中讀本身的同樣,不須要bundle參數,必定要傳的話,就傳nil或者[NSBundle mainBundle]code

4.從framework裏面讀其餘framwork裏面的資源文件

一樣也是利用bundle參數來讀取,class選擇目標framework中某個導出的class就能夠了。htm

 

小結blog

在單體程序中,NSBundle這個參數不須要管,所有傳nil或者是默認的[NSBundle mainBundle]就能夠了。接口

引入了framework以後,就須要NSBundle這個參數來區分資源所在的模塊。肯定NSBundle比較簡單的方法是用下面這個API,其中的class只要選擇資源所在的framework中的某個class就能夠了。獲取到bundle後,經過bundle獲取資源文件用法一致。資源

+ (NSBundle *)bundleForClass:(Class)aClass;
相關文章
相關標籤/搜索