打包靜態庫生成bundle流程
xcode 靜態庫資原始檔及xib封裝
目前有個需求,需要把.xib封裝到靜態庫中,試了很多種方法都不行,最後看到這篇文章 ,它是從 iOS Library With Resources 翻譯過來的,圖片沒有顯示(原文有)。
這裡主要是把靜態庫種的.xib封裝到bundle中,然後把靜態庫內鏈到工程中。
以下是這篇文章主要流程:
1 新增資源Bundle Target
資源必須是單獨編譯成Bundle才能使用,所以需要為MyLibrary(靜態庫target)專案生成另一個Bundle Target
選擇MyLibrary(靜態庫target)專案,在右邊面板中點Add Target->MAC OS X->FRAMEWORK &LIBRARY->Bundle
需要說明,bundle專案只有MAC OS X裡才有,但實際上也是可以用於IOS的,只是需要做些改動,即下面的第2步.
2 修改資源bundle target
把bundle target修改成ios能用的,看圖
Build Settings設定
1)Architectures為Standard (armv7)
2)Build Active Architecture Only 為指定的IOS版本
3 新增 XIB的輸出
設定XIB為輸出到BUNDLE,這一步相當於VS c#開發裡把某個圖片設定編譯動作為嵌入資源.
選擇Mylibrary(靜態庫target)專案,targets選擇MyLibraryResources(靜態庫資源target),Build Phases面板
Copy Bundle Resources點+選擇xib檔案
4 關聯靜態庫引用
把主程式和靜態庫做關聯,這樣省得每次要手動新增靜態庫的.a檔案
要說明下:我原本以為編譯主程式會自動編譯靜態庫,實際上做的並沒有那麼好,我改動了靜態庫的XIB內容,但編譯主程式後發現內容還是上次的,所以我每次都是clean build.
編輯Scheme(選單Product->Edit Scheme)
選擇Build
點+,選擇MyLibrary(靜態庫target)和MylibraryResources(靜態庫資源target)專案
這樣的話,每次build都可以編譯多個target和scheme。
5 最後設定主程式專案
選擇MyLibraryResource(靜態庫資源target)專案在Build Settings面板裡
設定User Header Search Paths為$(BUILT_PRODUCTS_DIR),注意建議把Recursive勾上(遞迴搜尋)
6 使用
選擇要使用靜態庫的工程,target->build phases->copy bundle resources,點選"+",add other 新增MylibraryResources.bundle,一般在build->release-iphoneos目錄。
重寫controller的init
ResourcesBundle=資原始檔bundle
- (id)init
{
NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundlemainBundle] URLForResource:ResourcesBundlewithExtension:@"bundle"]];
if ((self = [super initWithNibName:[NSStringstringWithUTF8String:object_getClassName(self)] bundle:bundle]))
{
//code
}
return self;
}
關於xib中的圖片
如果xib中有圖片,可能會報以下的警告,然後圖片顯示不成功。
Could not load the image 'xxx.png' referenced from a nib in the bundle with identifier "com.xxx.xxx"
解決方法:
手動指定xib中的圖片