創(chuàng)建遠程私有管理庫
-
在遠程服務(wù)器(例:碼云、Coding等)創(chuàng)建私有管理庫
- 碼云:https://gitee.com
- Coding: https://coding.net
就是新建一個私有項目
-
本地添加私有管理庫
-
$ pod repo add HQSpecs git@gitee.com:H-Joe/HQSpecs.git7.png
-
創(chuàng)建私有庫(組件)
-
創(chuàng)建私有庫(組件)
-
創(chuàng)建模版庫:
$ pod lib create HQBase使用模版庫的好處是,會自動生成測試工程,可以邊開發(fā)庫邊測試。
如果創(chuàng)建不成功,可能原因之一是
Xcode和cocoapods版本不匹配,升級cocoapods:$ sudo gem install cocoapods -
設(shè)置配置信息
8.png
9.png -
生成的模版如下:
10.png -
把要做成私有庫(組件)的代碼放入
Classes中,把其他資源文件(圖片、文件等)放在Assets中
11.png -
在模版中的測試工程
Example中,已經(jīng)配置好了Podfile,只需pod install即可
12.png
- 如果私有庫(組件)中有依賴系統(tǒng)庫,則需配置
s.frameworks或s.library - 如果私有庫(組件)中有依賴第三方庫,類似
SDWebImage、AFNetworking等,需要在HQBase.podspec中添加依賴關(guān)系
13.png
- 本地測試
-
-
推送到遠程服務(wù)器
-
在遠程服務(wù)器上創(chuàng)建私有庫(組件)項目
14.png -
配置
HQBase.podspec文件homepage是私有庫(組件)倉庫的地址,source是私有庫(組件)代碼地址15.png -
本地驗證
HQBase.podspec配置是否正確$ pod lib lint- 或
$ pod lib lint --allow-warnings
16.png -
提交遠程倉庫
-
git管理$ git add .$ git commit -m "描述信息"$ git remote add 遠程代碼地址$ git push origin master
-
增加標簽
$ git tag '0.1.0'
此標簽號必須與
HQBase.podspec中的s.version一致$ git push --tags
-
遠程驗證:
$ pod spec lint --allow-warnings
17.png
-
-
-
推送到遠程私有管理庫
$ pod repo push 遠程私有管理庫 本地個人私有庫.podspec-
例:
$ pod repo push HQSpecs HQBase.podspec18.png
-
使用
可以通過
pod search來搜索查詢私有組件-
$ pod search HQBase
19.png20.png -
在宿主項目,直接在
Podfile文件中配置
21.png
需要注意的是,必須配置兩個源,這樣就可以同時使用公有庫和私有庫
升級私有庫(組件)
-
添加代碼到
Classes中
22.png -
本地測試
- 在
Example中$ pod install - 編譯測試通過
- 在
-
推送到遠程服務(wù)器
-
配置
HQBase.podspec文件,version版本增加
23.png -
git管理$ git add .$ git commit -m "描述信息"$ git push origin master
-
增加標簽
$ git tag '0.2.0'$ git push --tags
遠程驗證:
$ pod spec lint --allow-warnings
-
-
推送到遠程私有管理庫
$ pod repo push HQSpecs HQBase.podspec
-
使用
- 宿主項目中,
$ pod update即可。
- 宿主項目中,
子私有庫(組件)
如果私有庫(組件)有很多功能,我們所用的只是其中的一個子功能,就有必要創(chuàng)建子私有庫(組件)
- 配置
HQBase.podspec文件,添加子私有庫(組件)增加版本號
version-
設(shè)置依賴
dependency依賴放在具體的子索引庫中,誰需要,誰設(shè)置,不需要,不設(shè)置
-
增加
subspec
24.png 本地驗證:
$ pod lib lint --allow-warnings
- 推送到遠程服務(wù)器
-
git管理$ git add .$ git commit -m "描述信息"$ git push origin master
- 增加標簽
$ git tag '0.3.0'$ git push --tags
- 遠程驗證:
$ pod spec lint --allow-warnings
-
- 推送到遠程私有管理庫
$ pod repo push HQSpecs HQBase.podspec
- 使用
-
$ pod search HQBase26.png -
宿主項目中,可以
$ pod install需要的子功能
25.png
-
私有庫(組件)中資源文件的使用
-
xib
-
在私有庫(組件)中加載
xib文件需注意以下幾點:-
- 所有使用
bundle的地方都必須動態(tài)獲取
[NSBundle bundleForClass:[self class]]27.png - 所有使用
-
- 不能使用
mainBundle, 因為此時的xib文件不在mainBundle下,是在xxx.framework下
28.png - 不能使用
-
-
使用
UILabel *l = [[UILabel alloc]init]; l.frame = CGRectMake(30, 20, 300, 30); l.text = @"1、測試加載xib文件"; [self.view addSubview:l]; TestXibView *t = [[NSBundle bundleForClass:[self class]] loadNibNamed:@"TestXibView" owner:nil options:nil].firstObject; t.frame = CGRectMake(30, 60, 300, 100); [self.view addSubview:t];
-
- 圖片
-
圖片資源放置的位置是
Assets
30.png -
配置
HQMain.podspec文件
31.png -
在私有庫(組件)中,圖片資源的存放位置和
xib文件一樣,已經(jīng)不在mainBundle下,是在xxx.framework下的xxx.bundle中32.png -
使用
-
-
xib中使用
- 在圖片名稱前面添加組件主
bundle, 本例中就是HQMain.bundle
33.pngUILabel * l = [[UILabel alloc]init]; l.frame = CGRectMake(30, 180, 300, 30); l.text = @"2、測試加載xib上有圖片"; [self.view addSubview:l]; TestXibImageView * t = [[NSBundle bundleForClass:[self class]] loadNibNamed:@"TestXibImageView" owner:nil options:nil].firstObject; t.frame = CGRectMake(100, 220, 120, 100); [self.view addSubview:t]; -
-
- 代碼加載
- 不能使用
imageNamed方法 - 使用
imageWithContentsOfFile方法 -
核心代碼
35.png
UILabel * l = [[UILabel alloc]init]; l.frame = CGRectMake(30, 340, 300, 30); l.text = @"3、測試加載圖片"; [self.view addSubview:l]; NSBundle *currentBundle = [NSBundle bundleForClass:[self class]]; NSString *bundleName = [currentBundle.infoDictionary[@"CFBundleName"] stringByAppendingString:@".bundle"]; NSString *path = [currentBundle pathForResource:@"aixin.png" ofType:nil inDirectory:bundleName]; UIImage *image = [UIImage imageWithContentsOfFile:path]; UIImageView * i = [[UIImageView alloc]init]; i.image = image; i.frame = CGRectMake(30, 380, 300, 250); [self.view addSubview:i];
-
在加載圖片時,圖片名稱必須是全名, 如:
aixin.png、aixin@2x.png、aixin@3x.png


























