一、創(chuàng)建本地私有庫
1、項(xiàng)目目錄中,新建文件夾Lib進(jìn)入,然后執(zhí)行pod lib create 庫名,根據(jù)提示進(jìn)行模板創(chuàng)建庫;例:
pod lib create FXGBaseKit
2、第一步完成之后,會(huì)有個(gè)子工程,可在Classes文件夾中存放代碼,Assets中存放圖片資源等;
(注1:加載圖片資源中,需要在XXX.podspec文件中配置,具體格式后面會(huì)貼上來;)
(注2:圖片加載方式也不能再用[UIImage imageName:@""],需要指定bundle加載圖片)
3、抽離代碼,在原有項(xiàng)目工程中的Podfile文件中導(dǎo)入本地路徑,例:
pod 'FXGBaseKit', :path => 'Lib/FXGBaseKit'
(注:新版的cocoapods需要在開頭加上source路徑)
新版Podfile格式范例:
use_frameworks!
platform :ios, '9.0'
source 'https://github.com/CocoaPods/Specs.git'
target 'XXX' do
pod 'FXGBaseKit', :path => 'Lib/FXGBaseKit(你的本地私有庫路徑)'
target 'XXX_Tests' do
inherit! :search_paths
end
end
備注:本地私有庫的創(chuàng)建比較簡(jiǎn)單,此處不再過多敘述。
二、創(chuàng)建遠(yuǎn)程私有庫
1、工程創(chuàng)建
pod lib create FXGBaseKits
2、新建遠(yuǎn)程倉庫(GitHub/GitLab/coding等)
3、將遠(yuǎn)程索引庫關(guān)聯(lián)到本地
pod repo (查看本地已關(guān)聯(lián)倉庫源)
pod repo add 本地索引庫名稱 遠(yuǎn)程索引庫倉庫地址 (添加關(guān)聯(lián)私有庫)
pod repo update 索引庫名稱 (更新私有庫)
pod repo remove 本地索引庫名稱 (移除私有庫)
4、進(jìn)入到本地項(xiàng)目工程FXGBaseKits中,關(guān)聯(lián)遠(yuǎn)程倉庫
git remote add origin 你的倉庫地址
git add .
git commit -m "commit"
git push origin master 或者 git push -u origin master 【這個(gè)一般是第一次提交的時(shí)候】
5、修改XXX.podspec文件,簡(jiǎn)易的格式及名詞解釋(請(qǐng)勿直接復(fù)制):【此處為注釋】
Pod::Spec.new do |s|
s.name = 'FXGBaseKits'【庫名】
s.version = '0.1.0' 【版本號(hào)】
s.summary = '' 【短描述】
s.description = <<-DESC
(此處需要填寫)【詳細(xì)的描述,需要比短描述長(zhǎng)】
DESC
s.homepage = 'https://github.com/xxx' 【你倉庫所在的網(wǎng)頁網(wǎng)址,不是倉庫地址】
s.license = { :type => 'MIT', :file => 'LICENSE' }【MIT許可證,一般不用動(dòng)】
s.author = { 'xxx' => 'xxx' } 【作者名,默認(rèn)會(huì)給你,一般也不需要?jiǎng)印?
s.source = { :git => 'https://github.com/xxx.git', :tag => s.version.to_s } 【你倉庫的Git地址】
s.ios.deployment_target = '9.0' 【系統(tǒng)版本號(hào)】
s.source_files = 'xxx/Classes/*' 【代碼存放的路徑,是相當(dāng)于XXX.podspec的路徑,一定要正確,否則會(huì)報(bào)錯(cuò)】
# s.resource_bundles = {
# 'XXX' => ['XXX/Assets/*.png'] 【這是圖片資源的路徑】
# }
# s.public_header_files = 'Pod/Classes/**/*.h' 【在這個(gè)屬性中聲明的.h文件能夠使用<>方法聯(lián)想調(diào)用】
# s.frameworks = 'UIKit', 'MapKit' 【依賴系統(tǒng)的動(dòng)態(tài)庫】
# s.vendored_framework = 'XXX.framework' 【依賴非系統(tǒng)的動(dòng)態(tài)庫,未驗(yàn)證】
# s.libraries = 'XXX.a' 【依賴系統(tǒng)的靜態(tài)庫】
# s.vendored_libraries = 'XXX.a' 【依賴非系統(tǒng)的靜態(tài)庫,例:libWeiboSDK.a】
s.dependency 'AFNetworking', '~> 4.0.1' 【依賴的pod庫】
end
注:當(dāng)有多級(jí)庫的時(shí)候,可分解出多個(gè)子庫,只需要將s.source_files = 'xxx/Classes/*'注釋,在XXX.podspec添加以下代碼:
s.subspec 'Category' do |c|【此處"||"中的字母不能寫大寫的,注意】
c.source_files = 'FXGBaseKit/Classes/Category/**/*'【子庫的路徑】
end
s.subspec 'General' do |g|
g.source_files = 'FXGBaseKit/Classes/General/**/*'【子庫的路徑】
end
s.subspec 'Utils' do |u|
u.source_files = 'FXGBaseKit/Classes/Utils/**/*'【子庫的路徑】
end
s.subspec 'Vendor' do |v|
v.source_files = 'FXGBaseKit/Classes/Vendor/**/*'【子庫的路徑】
end
6、驗(yàn)證XXX.podspec文件,終端執(zhí)行:
pod lib lint XXX.podspec --allow-warnings【本地驗(yàn)證】
pod spec lint XXX.podspec --allow-warnings 【遠(yuǎn)程驗(yàn)證】
注 ①:若使用了靜態(tài)庫,需要在后面加 --use-libraries;
注 ②:當(dāng)依賴了其他第三方庫,需要在--allow-warnings 前面加上 --source='https://xxx.git【你的倉庫地址】, https://github.com/CocoaPods/Specs.git'
7、當(dāng)XXX.podspec驗(yàn)證通過后,給遠(yuǎn)程master分支添加tag
git tag 0.1.0 【添加本地tag】
git push --tags 【push到遠(yuǎn)程tag】
git tag -d 0.1.0 【刪除本地tag】
git push origin :0.1.0 【刪除遠(yuǎn)程tag】
git tag 【查看tag列表】
8、給master設(shè)置完tag之后,執(zhí)行發(fā)布命令
pod repo push XXX XXX.podspec --allow-warnings
注 ①:若使用了靜態(tài)庫,需要在后面加 --use-libraries;
注 ②:當(dāng)依賴了其他第三方庫,需要在--allow-warnings 前面加上 --source='https://XXX.git【你的倉庫地址】, https://github.com/CocoaPods/Specs.git'
9、發(fā)布完成之后,私有庫便已經(jīng)創(chuàng)建完畢,附上使用方法:
新建工程,編輯Podfile,編輯完畢后,pod install或者pod update;
注意:私有庫需要指定git的寫法,否則可能會(huì)install不成功,備注:當(dāng)?shù)谝淮问褂玫臅r(shí)候,需要先更新本地索引庫;
pod repo update 【更新所有的索引庫】
pod repo update xxx 【指定更新某個(gè)索引庫】
指定git的寫法
pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git'
pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git', :branch => 'dev'
pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => '3.1.1'
pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git', :commit => '0f506b1c45'
指定podspec的寫法
pod 'AFNetworking', :podspec => '~/Desktop/AFNetworking/AFNetworking.podspec'
指定subspecs的寫法(這也是subspec的另一個(gè)用法)
pod 'MyThirdparty', '~> 0.0.1', :subspecs => ['Sparta', 'GotyeSDK', 'TalkingData', 'Tingyun', 'BaiduPanorama']