1. 創(chuàng)建bundle伴挚,如圖,點(diǎn)擊 + ,彈出選擇框砾层, macOS 下的Framework & Library 托慨,點(diǎn)擊bundle左胞,輸入bundle的名字瑰抵,然后點(diǎn)擊 finish你雌。
2. 點(diǎn)擊創(chuàng)建好的bundle ,修改屬性
"Base SDK" 設(shè)置為 "Latest iOS (iOS 11.2)" (Xcode 9.2為例)
"Build Active Architecture Only" 設(shè)置為 "YES"
Installation Directiotory 刪除掉后面的路徑
Code Signing Identity 選擇 Don't Code Sign
"iOS Deployment Target" 設(shè)置為 iOS 8.0 (為了兼容性二汛,最好選擇最低版本)
"Skip Install" 設(shè)置為 "NO"
"Strip Debug Symbols During Copy" 中"Release"模式設(shè)置為 "YES"
"IOS Deployment Target" 設(shè)置為 "iOS 8.0"
"COMBINE_HIDPI_IMAGES" 設(shè)置為 "NO"
3. 現(xiàn)在開始導(dǎo)入資源婿崭,可以導(dǎo)入Xib文件和圖片,此處以導(dǎo)入圖片為例习贫,點(diǎn)擊?進(jìn)行添加逛球,如圖3.1;或者直接將圖片拖入左側(cè)創(chuàng)建的bundle文件夾下苫昌,系統(tǒng)會(huì)自動(dòng)導(dǎo)入Copy Bundle Resources里去,如張圖3.2所示:
4. 選擇創(chuàng)建的bundle 進(jìn)行編譯幸海,開始生成bundle祟身,分別選擇真機(jī)和模擬器,然后各運(yùn)行一遍物独,即可生成真機(jī)和模擬器使用的bundle:
5. 找到生成的bundle袜硫,打包上架APP的時(shí)候應(yīng)使用真機(jī)模式下運(yùn)行生成的Bundle,即Debug-iPhoneos 文件夾內(nèi)的bundle挡篓。
現(xiàn)在已經(jīng)生成了我們需要的bundle婉陷,在項(xiàng)目中直接導(dǎo)入即可使用,
6. 將要使用的bundle集成到項(xiàng)目中后官研,就可以使用了秽澳。需要注意的就是,bundle是靜態(tài)的戏羽,不進(jìn)行編譯的資源文件担神。所以,要使用bundle中的資源始花,就需要找到相應(yīng)的資源路徑妄讯。
VC獲得bundle中的資源
NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "MyBundle"ofType :@ "bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name"bundle:resourceBundle];
圖片獲得bundle中的資源
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50,50)];
UIImage *image = [UIImage imageNamed:@"MyBundle.bundle/img_collect_success"];
[imgView setImage:image];
或者
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50,50)];
NSString *bundlePath = [[ NSBundle mainBundle] pathForResource:@"Test" ofType :@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *img_path = [bundle pathForResource:imageName ofType:@"png"];
UIImage *image_1=[UIImage imageWithContentsOfFile:img_path];
[imgView setImage:image_1];
當(dāng)然,可以寫成預(yù)編譯語句:
#define MYBUNDLE_NAME @ "MyBundle.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
#define MYBUNDLE_NAME_2 @"BundleTest2.bundle"
#define MYBUNDLE_PATH_2 [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME_2]
#define MYBUNDLE_2 [NSBundle bundleWithPath: MYBUNDLE_PATH_2]
#define MYBUNDLE_NAME_3 @"Bundle3.bundle"
#define MYBUNDLE_PATH_3 [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME_3]
#define MYBUNDLE_3 [NSBundle bundleWithPath: MYBUNDLE_PATH_3]
// 從bundle中加載xib
UIView *view = [[MYBUNDLE_2 loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0];
view.frame = self.view.frame;
[self.view addSubview:view];
// 從bundle中加載圖片
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageWithContentsOfFile:[MYBUNDLE_PATH_2 stringByAppendingPathComponent:@"Contents/Resources/share_pengyouquan"]];
[view addSubview:imageView];
// 從另一個(gè)bundle中加載storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Bundle3Storyboard" bundle:MYBUNDLE_3];
NSLog(@"storyboard = %@",storyboard);
// 從另一個(gè)bundle中加載圖片
UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(250, 100, 100, 100)];
imageView2.image = [UIImage imageWithContentsOfFile:[MYBUNDLE_PATH_3 stringByAppendingPathComponent:@"Contents/Resources/guanyudingyue"]];
[view addSubview:imageView2];
7. 如果將自己打包的bundle給別人使用酷宵,別人在打包上傳過程中可能會(huì)遇到錯(cuò)誤提示如:
ERROR ITMS-90171: "Invalid Bundle Structure - The binary file 'lhby.app/Test.bundle/Test' is not permitted. Your app can’t contain standalone executables or libraries, other than a valid CFBundleExecutable of supported bundles. Refer to the Bundle Programming Guide at...
或者
ERROR ITMS-90535: "Unexpected CFBundleExecutable Key. The bundle at 'Payload/dianlan2.app/EaseUIResource.bundle' does not contain a bundle executable. If this bundle intentionally does not contain an executable, consider removing the CFBundleExecutable key from its Info.plist and using a CFBundlePackageType of BNDL. If this bundle is part of a third-party framework, consider contacting the developer of the framework for an update to address this issue."
或者
ERROR ITMS-90034: "Missing or invalid signature. The bundle 'ABC.Test' at bundle path 'Payload/lhby.app/Test.bundle' is not signed using an Apple submission certificate."
網(wǎng)上也有很多的解決辦法亥贸,這里提供一種解決方法,就是刪除bundle里的執(zhí)行文件:找到工程中的Test.Bundle浇垦,右鍵單擊后 選擇 "顯示包內(nèi)容"炕置,找到里面黑色的可執(zhí)行文件Test,刪除掉,然后找到里面的info.plist文件 讹俊,刪除掉Executable file 字段垦沉,重新打包,上傳應(yīng)用商店就可以了仍劈。
參考文章:
iOS 把圖片資源打包成bundle
iOS_Bundle資源文件包
【Xcode小技巧】生成Bundle包
iOS-生成Bundle包-引入bundle-使用bundle