如何在Framework中讀取bundle中的Res


前因:

  因為公司上架前后的原因,外圍的平台層部分提前上線,而我做的功能部分需要晚一些上線,是單獨的一個工程在其他倉庫開發。

我的資源文件放在Bundle中。合到主工程中,資源文件不用改,直接拖進去。倒是代碼部分因為重名較多,花了大半天時間來改名字。

  過一段時間,需要將我們的代碼以Framework的形式,放入另一個項目的平台層中。我的做法是,將代碼打包進入Framework,然后資源文件在Bundle中,兩部分拖進平台層,資源文件調用等等也都不用改。

因為Android就只有一個aar文件導入平台層。所以對方提出希望我也只提供一個Framework 包。

所以現在就需要改資源文件調用方式了。

以前的做法很簡單:

NS_INLINE UIImage * UIResourceBundleSubMove(NSString *strPath){
    return [UIImage imageNamed:[NSString stringWithFormat:@"XXXX.bundle/images/move/%@.png",strPath]];
}

 

現在就是

#define FrameworkPath  [[NSBundle mainBundle] pathForResource:@"VivenSDK" ofType:@"framework"]
#define FrameworkBundle  [NSBundle bundleWithPath:FrameworkPath]
#define VivienBundle [NSBundle bundleWithPath:[FrameworkBundle pathForResource:@"Vivien" ofType:@"bundle"]] #define UIResourceBundleSubMove(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"images/move/%@",imageName] inBundle:VivienBundle compatibleWithTraitCollection:nil]

NS_INLINE UIImage * UIResourceBundleMore(NSString *imageName){ return [UIImage imageNamed:[NSString stringWithFormat:@"images/main/%@",imageName] inBundle:VivienBundle compatibleWithTraitCollection:nil]; }

 

然后再把Framework再拖一份到主工程的Copy Bundle Resources

 

如果報錯,可以通過代碼的方式查看報錯:

    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"VivienSDK" ofType:@"framework"];
    NSLog(@"path = %@", path);
    
    NSBundle *myBundle = [NSBundle bundleWithPath:path];
    NSLog(@"myBunlde = %@", myBundle);
    
    NSBundle *vivienResBundle= [NSBundle bundleWithPath:[myBundle pathForResource:@"Vivien" ofType:@"bundle"]];
    
    NSLog(@"vivienResBundle = %@", vivienResBundle);
    
    UIImage *iconImage = [UIImage imageNamed:[NSString stringWithFormat:@"Images/%@",@"icon_friend"] inBundle:vivienResBundle compatibleWithTraitCollection:nil];
    NSLog(@"iconImage = %@", iconImage);

 

現在發現這樣有問題,Archive的時候會報錯:

 Failed to generate distribution items with error: Error Domain=DVTMachOErrorDomain Code=0 "Found an unexpected Mach-O header code: 0x72613c21" 
UserInfo={NSLocalizedDescription=Found an unexpected Mach-O header code: 0x72613c21, NSLocalizedRecoverySuggestion=}

因為,靜態的framework不能打包進bundle。靜態庫會編譯進二進制文件的。靜態framework里面的資源需要重新打包一個bundle。工程里面鏈接下framework,加入資源bundle就ok了 

 

如果改為動態庫可以成功;

1,Sandbox會驗證動態庫的簽名,所以如果是動態從服務器更新的動態庫,是簽名不了的,因此應用插件化、軟件版本實時模塊升級等功能在iOS上無法實現;

http://www.jianshu.com/p/f2ffe8325519 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM