CocoaPods 創(chuàng)建私有庫(kù)

創(chuàng)建私有Spec Repo

先建立一個(gè)私有g(shù)it倉(cāng)庫(kù)频祝,用來(lái)存放podspec口锭。我是建在碼云上。

然后在終端執(zhí)行命令:

$ pod repo add FCPrivateRepo https://gitee.com/sergeant/fcprivate-repo.git

執(zhí)行成功后糊肠,可以在~/.cocoapods/repos目錄下看到FCPrivateRepo文件夾闸盔。

也可以在終端執(zhí)行命令查看所有的repo:

$ pod repo

FCPrivateRepo
- Type: git (master)
- URL:  https://gitee.com/sergeant/fcprivate-repo.git
- Path: /Users/zhijiazhong/.cocoapods/repos/FCPrivateRepo

master
- Type: git (master)
- URL:  https://github.com/CocoaPods/Specs.git
- Path: /Users/zhijiazhong/.cocoapods/repos/master

trunk
- Type: CDN
- URL:  https://cdn.cocoapods.org/
- Path: /Users/zhijiazhong/.cocoapods/repos/trunk

3 repos

創(chuàng)建私有組件

我下面新建一個(gè)項(xiàng)目FCPrivateComponentA:

$ pod lib create FCPrivateComponentA

執(zhí)行過(guò)程中會(huì)問(wèn)以下問(wèn)題悯辙,按需回答即可:

What platform do you want to use?? [ iOS / macOS ]
 > ios

What language do you want to use?? [ Swift / ObjC ]
 > objc

Would you like to include a demo application with your library? [ Yes / No ]
 > yes

Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > none

Would you like to do view based testing? [ Yes / No ]
 > no

What is your class prefix?
 > FC

完成之后就可以添加組件代碼了,加在FCPrivateComponentA/Classes目錄下迎吵。

然后我們需要再創(chuàng)建一個(gè)私有倉(cāng)庫(kù)存放這個(gè)組件:

https://gitee.com/sergeant/fcprivate-component-a.git

之后我們就可以編輯podspec文件了躲撰。

至少需要修改以下4個(gè)字段:

- s.summary                 # 簡(jiǎn)介
- s.description             # 詳細(xì)描述,要比簡(jiǎn)介長(zhǎng)
- s.homepage                # 主頁(yè)
- s.source                  # 倉(cāng)庫(kù)地址

編輯完如下:

#
# Be sure to run `pod lib lint FCPrivateComponentA.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'FCPrivateComponentA'
  s.version          = '0.1.0'
  s.summary          = 'A private pod for test.'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
A private pod which show a view controller with red background.
                       DESC

  s.homepage         = 'https://gitee.com/sergeant/fcprivate-component-a'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Finger Chung' => '260910416@qq.com' }
  s.source           = { :git => 'https://gitee.com/sergeant/fcprivate-component-a.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'FCPrivateComponentB/Classes/**/*'
  
  # s.resource_bundles = {
  #   'FCPrivateComponentB' => ['FCPrivateComponentB/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

然后我們就可以提交代碼了钓觉。先在.gitignore文件中忽略Pod/文件夾和.xcworkspace文件茴肥。
然后執(zhí)行命令:

$ git add .
$ git commit -s -m "Initial Commit of Library"
$ git remote add origin https://gitee.com/sergeant/fcprivate-component-a.git           #添加遠(yuǎn)端倉(cāng)庫(kù)
$ git push origin master     #提交到遠(yuǎn)端倉(cāng)庫(kù)

因?yàn)閜odspec文件中獲取Git版本控制的項(xiàng)目還需要tag號(hào),所以我們要打上一個(gè)tag:

$ git tag -m "first release" 0.1.0
$ git push --tags     #推送tag到遠(yuǎn)端倉(cāng)庫(kù)

做完上面的工作荡灾,私有組件就做好了瓤狐,但是我們要驗(yàn)證一下是否可用。使用命令:

pod lib lint

執(zhí)行后可能出現(xiàn)警告導(dǎo)致驗(yàn)證不通過(guò):

$ pod lib lint

 -> FCPrivateComponentA (0.1.0)
    - WARN  | url: The URL (https://gitee.com/sergeant/fcprivate-component-a) is not reachable.

如果跟我一樣提示主頁(yè)不可訪問(wèn)批幌,可能是因?yàn)槲覀兪褂玫氖撬接袔?kù)础锐,直接忽略就好:

$ pod lib lint --allow-warnings

 -> FCPrivateComponentA (0.1.0)
    - WARN  | url: The URL (https://gitee.com/sergeant/fcprivate-component-a) is not reachable.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'FCPrivateComponentA' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')

FCPrivateComponentA passed validation.

這樣就驗(yàn)證通過(guò)了。

如果是其他警告荧缘,則需要一一修改皆警。一般都是podspec文件不規(guī)范導(dǎo)致的。
比如我一開(kāi)始沒(méi)有修改s.summary截粗,第二次s.description比s.summary短信姓,都被警告了鸵隧。

驗(yàn)證通過(guò)后,就可以嘗試使用了意推。

本地測(cè)試

先在本地測(cè)試下組件是否可用豆瘫。

新建一個(gè)工程。cd到工程根目錄菊值,然后使用命令 pod init 生成Podfile外驱。

使用本地路徑導(dǎo)入FCPrivateComponentA:

pod 'FCPrivateComponentA', :path => '../FCPrivateComponentA'

path 既可以使用相對(duì)路徑,也可以使用絕對(duì)路徑腻窒。

還可以用podsepc文件的路徑:

pod 'FCPrivateComponentA', :podspec => '../FCPrivateComponentA/FCPrivateComponentA.podspec'

然后執(zhí)行 pod install 安裝昵宇。

$ pod install
Analyzing dependencies
Fetching podspec for `FCPrivateComponentA` from `../FCPrivateComponentA/FCPrivateComponentA.podspec`
Downloading dependencies
Installing FCPrivateComponentA (0.1.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

打開(kāi)項(xiàng)目工程,可以看到庫(kù)文件都被加載到Pods子項(xiàng)目中了儿子,不過(guò)它們并沒(méi)有在Pods目錄下瓦哎,而是跟測(cè)試項(xiàng)目一樣存在于Development Pods/FCPrivateComponentA,這是因?yàn)槲覀兪窃诒镜販y(cè)試典徊,而沒(méi)有把podspec文件添加到Spec Repo中的緣故杭煎。

添加到Spec Repo

現(xiàn)在,我們最開(kāi)始創(chuàng)建的Spec Repo派上用場(chǎng)了卒落。

只需要一個(gè)命令即可:

$ pod repo push FCPrivateRepo FCPrivateComponentA.podspec  #前面是本地Repo名字 后面是podspec名字

執(zhí)行完后,我們可以在~/.cocoapods/repos/FCPrivateRepo目錄下看到FCPrivateComponentA蜂桶。

同樣儡毕,在FCPrivateRepo的遠(yuǎn)程倉(cāng)庫(kù)也可以看到FCPrivateComponentA.podspec被提交上去。

我們也可以搜索到這個(gè)組件:

$ pod search FCPrivateComponentA

-> FCPrivateComponentA (0.1.0)
   A private pod for test.
   pod 'FCPrivateComponentA', '~> 0.1.0'
   - Homepage: https://gitee.com/sergeant/fcprivate-component-a
   - Source:   https://gitee.com/sergeant/fcprivate-component-a.git
   - Versions: 0.1.0 [FCPrivateRepo repo]

到這里組件就制作完成了扑媚。

正式使用

重新打開(kāi)Podfile腰湾,將本地測(cè)試時(shí)指定的本地路徑去掉:

pod 'FCPrivateComponentA'

還需要在頭部加上我們的私有Spec Repo地址,注意不要寫(xiě)成了私有組件的倉(cāng)庫(kù):

source 'https://gitee.com/sergeant/fcprivate-repo.git'

完整的Podfile:

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

# 這里是私有索引庫(kù)的地址
# 而不是私有組件的地址 https://gitee.com/sergeant/fcprivate-component-a.git
source 'https://gitee.com/sergeant/fcprivate-repo.git'

target 'TestPrivatePod' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for TestPrivatePod
  pod 'FCPrivateComponentA'

end

執(zhí)行 pod update:

$ pod update
Update all pods
Updating local specs repositories
  $ /usr/bin/git -C /Users/sergeant/.cocoapods/repos/FCPrivateRepo fetch
  origin --progress
  $ /usr/bin/git -C /Users/sergeant/.cocoapods/repos/FCPrivateRepo rev-parse
  --abbrev-ref HEAD
  master
  $ /usr/bin/git -C /Users/sergeant/.cocoapods/repos/FCPrivateRepo reset
  --hard origin/master
  HEAD is now at 4941c37 [Add] FCPrivateComponentA (0.1.0)
Analyzing dependencies
Downloading dependencies
Installing FCPrivateComponentA 0.1.0
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

如有項(xiàng)目組的其他人需要使用疆股,需要給他對(duì)應(yīng)私有庫(kù)的權(quán)限费坊,然后也執(zhí)行命令:

$ pod repo add FCPrivateRepo https://gitee.com/sergeant/fcprivate-repo.git
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市旬痹,隨后出現(xiàn)的幾起案子附井,更是在濱河造成了極大的恐慌,老刑警劉巖两残,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件永毅,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡人弓,警方通過(guò)查閱死者的電腦和手機(jī)沼死,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)崔赌,“玉大人意蛀,你說(shuō)我怎么就攤上這事耸别。” “怎么了县钥?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵太雨,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我魁蒜,道長(zhǎng)囊扳,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任兜看,我火速辦了婚禮锥咸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘细移。我一直安慰自己搏予,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開(kāi)白布弧轧。 她就那樣靜靜地躺著雪侥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪精绎。 梳的紋絲不亂的頭發(fā)上速缨,一...
    開(kāi)封第一講書(shū)人閱讀 49,792評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音代乃,去河邊找鬼旬牲。 笑死,一個(gè)胖子當(dāng)著我的面吹牛搁吓,可吹牛的內(nèi)容都是我干的原茅。 我是一名探鬼主播,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼堕仔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼擂橘!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起摩骨,我...
    開(kāi)封第一講書(shū)人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤通贞,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后仿吞,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體滑频,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年唤冈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了峡迷。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖绘搞,靈堂內(nèi)的尸體忽然破棺而出彤避,到底是詐尸還是另有隱情,我是刑警寧澤夯辖,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布琉预,位于F島的核電站,受9級(jí)特大地震影響蒿褂,放射性物質(zhì)發(fā)生泄漏圆米。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一啄栓、第九天 我趴在偏房一處隱蔽的房頂上張望娄帖。 院中可真熱鬧,春花似錦昙楚、人聲如沸近速。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)削葱。三九已至,卻和暖如春淳梦,著一層夾襖步出監(jiān)牢的瞬間析砸,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工谭跨, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留干厚,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓螃宙,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親所坯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子谆扎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348