前言
最近公司準(zhǔn)備開發(fā)一個(gè)模塊,在App首頁添加個(gè)入口.想到公司的項(xiàng)目已經(jīng)很龐大了,每次改一點(diǎn)東西都要整個(gè)工程運(yùn)行,很浪費(fèi)時(shí)間,同時(shí)考慮到后期其他定制類的項(xiàng)目有可能也要用到這個(gè)模塊,又要粘貼復(fù)制(前面一直這樣做的,很惡心).所以就想這把這個(gè)模塊作為私有庫,想用的時(shí)候直接pod下載.
制作過程
-
創(chuàng)建代碼私有庫
去GitHub創(chuàng)建一個(gè)代碼庫(可以用自己公司的git服務(wù)),創(chuàng)建的時(shí)候新建一下LICENSE文件MIT類型, 然后clone下來,比如我創(chuàng)建的地址是:https://github.com/scrumsnail/HLInterTestPod.git
然后把自己模塊代碼push上去.終端命令是
git add .
git commit -m 'log日志'
git pull
git push
這是我們平常代碼控制操作.
-
創(chuàng)建私有podspec文件
找到自己剛剛clone下來的文件,cd進(jìn)去,然后執(zhí)行
pod spec create HLInterTestPod
HLInterTestPod這個(gè)是你要?jiǎng)?chuàng)建的私有庫名字.然后你會(huì)發(fā)現(xiàn)生成了一個(gè).podspec文件,用文本編輯打開,根據(jù)自己情況進(jìn)行修改,不讓pod不會(huì)成功,可以全部刪掉,然后替換下面內(nèi)容再進(jìn)行修改
Pod::Spec.new do |spec|
spec.name = "HLInterTestPod" # 私有庫名稱
spec.version = "1.0.9" # 私有庫版本號(hào)
spec.summary = "這是護(hù)理的私有庫" # 項(xiàng)目簡介
spec.description = <<-DESC
"this is hull pod"
DESC # 項(xiàng)目簡介
spec.homepage = "https://github.com/scrumsnail/HLInterTestPod" # 倉庫的主頁
spec.license = "MIT" # 開源許可證
spec.author = { "luyoudui" => "3269190984@qq.com" } # 作者信息
spec.platform = :ios, "9.0" #平臺(tái)及支持的最低版本
spec.source = { :git => "https://github.com/scrumsnail/HLInterTestPod.git", :tag => "#{spec.version}" } #倉庫地址
spec.source_files = "HLInterTestPod/HLInterTestPod/Classes/**/*" #代碼位置,路徑一定要弄對
spec.resource_bundles = {
'HLInterTestPod' => ["HLInterTestPod/HLInterTestPod/Assets/*"]
}#資源包,如果私有庫中要用到圖片和xib等,路徑要對
spec.exclude_files = "HLInterTestPod/HLInterTestPod/AppDelegate.{h,m}", "HLInterTestPod/HLInterTestPod/main.m" #過濾文件,這些文件將不會(huì)上傳
spec.dependency "MBProgressHUD" # 依賴庫,每一個(gè)依賴都要寫
spec.requires_arc = true #是否支持arc
end
-
提交代碼
提交之前先驗(yàn)證一下自己創(chuàng)建的podspec文件對不對
pod lib lint
沒問題顯示 HLInterTestPod passed validation
然后就可以放心的提交代碼了
git add .
git commit -m 'log日志'
git tag '1.0.0' //這里版本號(hào)很重要,一定要跟podspec文件中的version相對應(yīng)
git push --tags //推送到倉庫
git push
pod trunk push // 發(fā)布podsepc,這里如果沒有注冊trunk,要先注冊,下面關(guān)于這個(gè)做一個(gè)介紹
然后在主工程引用 pod 'HLInterTestPod','~>1.0.9'
-
pod trunk問題
沒有注冊過trunk的,先注冊
pod trunk register 3269190984@qq.com
然后你有郵箱將會(huì)收到一條信息,驗(yàn)證一下即可
驗(yàn)證
pod trunk me
添加同事一起維護(hù)這個(gè)私有庫
pod trunk add-owner HLInterTestPod 已注冊trunk的郵箱地址
移除某個(gè)維護(hù)人員
pod trunk remove-owner HLInterTestPod 已注冊trunk的郵箱地址
刪除已發(fā)的某個(gè)版本對應(yīng)的工程信息
pod trunk delete HLInterTestPod 版本號(hào)
xib問題
如果想要在主工程中引用私有庫中的xib,我們正常寫法直接push到控制器,這個(gè)時(shí)候不會(huì)顯示xib的.因?yàn)檫@個(gè)xib的bundle路徑不對.
- 解決方法
在主工程寫一個(gè)分類
// NSBundle+HLBundle.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSBundle (HLBundle)
+ (instancetype)hlBundleWithBundleName:(NSString *)bundleName targetClass:(Class)targetClass;
@end
NS_ASSUME_NONNULL_END
// NSBundle+HLBundle.m
#import "NSBundle+HLBundle.h"
@implementation NSBundle (HLBundle)
+ (instancetype)hlBundleWithBundleName:(NSString *)bundleName targetClass:(Class)targetClass {
//并沒有拿到子bundle
NSBundle *bundle = [NSBundle bundleForClass:targetClass];
//在這個(gè)路徑下找到子bundle的路徑
NSString *path = [bundle pathForResource:bundleName ofType:@"bundle"];
//根據(jù)路徑拿到子bundle
return path?[NSBundle bundleWithPath:path]:[NSBundle mainBundle];
}
@end
然后調(diào)用
HLHomeViewController *vc = [[HLHomeViewController alloc] initWithNibName:@"HLHomeViewController" bundle:[NSBundle hlBundleWithBundleName:@"HLInterTestPod" targetClass:[self class]]];
[self.navigationController pushViewController:vc animated:YES];
私有庫更新
當(dāng)你的私有庫需要修改或者添加?xùn)|西的時(shí)候,一定要去修改podspec的version,比上個(gè)version高,然后重復(fù)上面制作過程中的第3步提交代碼. 每一次執(zhí)行pod trunk push成功之后,想要在主工程pod的時(shí)候你會(huì)發(fā)現(xiàn)報(bào)錯(cuò),說沒有次版本的庫.(可能是要審核)
- 解決方法
等5分鐘左右,執(zhí)行 pod install --repo-update
以下是我的私有庫和主工程,大家可以下載下來對應(yīng)文檔看看,如有不足,請大家補(bǔ)充,歡迎討論
私有庫:https://github.com/scrumsnail/HLInterTestPod