前言
現在大部分的開發者都是用Cocopods來管理第三方庫,Cocoapods為庫的管理提供了便利。在項目的發展當中會出現很多公共組件,如果只是單純拷貝代碼,維護起來會變得非常麻煩,因此制作私有庫將給組件管理帶來極大遍歷,而Cocoapods也提供了這樣的功能。
文章分為兩部分,第一部分是私有庫的制作,第二部分介紹開源自己的庫,將自己的庫發布到Cocoapods公共倉庫。
新建倉庫
Cocoapods使用podspec來管理庫的信息,因此需要一個倉庫來管理這些podspec文件。如果要創建私有庫就需要一個一個自己的倉庫來存儲podspec文件,如果開源就可以上傳到Cocoapods公有的倉庫。因此,創建私有庫之前我們先創建私有的spec repo。
創建私有Spec Repo
創建spec的git倉庫
http://192.168.1.100:3000/AndrewShen/MySpecRepo.git
添加創建的
Spec Repo
使用命令行添加倉庫到本地repo,會自動建立git的鏈接
# pod repo add REPO_NAME SOURCE_URL
$ pod repo add MySpecRepo http://192.168.1.100:3000/AndrewShen/MySpecRepo.git
將會執行:
Cloning spec repo `MySpecRepo` from `http://192.168.1.100:3000/AndrewShen/MySpecRepo.git`
執行成功后可以執行pod repo
查看已有的repo
新建pod
從零開始創建pod
- 如果需要從頭開始寫項目,開發組件,那可以通過
pod lib create
來創建工程
我們執行
```Ruby
#pod lib create LIBNAME
pod lib create podDemo
```
之后會問你四個問題,問完后自動執行`pod install`并生成依賴:
1. 使用什么語言
2. 是否需要一個例子工程
3. 選擇一個測試框架
4. 是否基于View測試
5. 類的前綴
如果執行`pod install`失敗,則刪掉PodFile里的test target就行了。
- 創建成功后我們可以看到目錄結構,我們的pod會存在于Example項目的Pods的
Development Pods
目錄下。
```
podDemo
├── Example
│ ├── Podfile
│ ├── Podfile.lock
│ ├── Pods
│ ├── Tests
│ ├── podDemo
│ ├── podDemo.xcodeproj
│ └── podDemo.xcworkspace
├── LICENSE
├── README.md
├── _Pods.xcodeproj -> Example/Pods/Pods.xcodeproj
├── podDemo
│ ├── Assets
│ └── Classes
└── podDemo.podspec
```
3.向pod文件夾(此處為podDemo)中加入資源文件(Assets
中)和庫文件(Classes
中),
在example文件夾下執行
pod update
更新工程pod。成功后Development Pods
目錄下我們的pod文件便會更新。只要更新了pod文件夾下內容或者更改了podspec文件都需要執行pod update
通過Cocoapods創建的項目會在本地的git管理下,我們需要將其添加到遠程倉庫。
```ruby
git add .
git commit -m "Initial"
git remote add origin http://git.celebi.com/AndrewShen/podDemo.git #添加遠端倉庫
git push origin master #提交到遠端倉庫
```
- 由于
podspec
文件pod源需要tag
號,因此我們需要為項目打上tag
.
```ruby
git tag -a 0.0.1 -m "tag release 0.0.1" #打tag
git push --tags #提交tag
```
- 編輯
podspec文件
```Ruby
Pod::Spec.new do |s|
s.name = 'podDemo' #pod名稱
s.version = '0.1.0' #pod版本
s.summary = '測試pod簡介.' #簡介,需要更改,不然會報警告
# 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 #詳細介紹,要比簡介長
測試pod的詳細介紹
DESC
s.homepage = 'http://192.168.1.100:3000/AndrewShen/podDemo' # 項目主頁
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' } #協議
s.author = { 'AndrewShen' => 'andrewshen@minture.com' } # 開發者信息
s.source = { :git => 'http://git.celebi.com/AndrewShen/podDemo.git', :tag => s.version.to_s } #倉庫地址
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '7.0' # 最低版本
s.source_files = 'podDemo/Classes/**/*' # 庫文件
# s.resource_bundles = { #資源目錄
# 'podDemo' => ['podDemo/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
s.frameworks = 'UIKit', 'MapKit' #依賴的framework
# s.dependency 'AFNetworking', '~> 2.3' # 依賴的第三方庫
```
- 編輯后使用
pod lib lint
驗證podspec文件是否可用
。如果有Error
和Warning
是無法添加到spec repo
中的。但是Warning
可以存在,可以使用選項--allow-warnings
忽略警告。
```
pod lib lint
```
如果顯示為以下則說明創建成功
```Ruby
-> podDemo (0.1.0)
podDemo passed validation.
```
從組件項目中創建pod
如果已經有組件項目/Demo則只需要創建podspec文件。
- 執行命令創建podspec文件:
pod spec create ADSSegmentedButton https://github.com/Andrewmika/ADSSegmentedButton.git
創建完成后打開我們創建的podspec文件,進行編輯。
驗證文件是否可用
執行命令`pod lib lint`
如果顯示為以下則說明創建成功
```Ruby
-> ADSSegmentedButton (0.0.1)
ADSSegmentedButton passed validation.
```
pod本地測試
配置好podspec
文件后可以在本地測試pod是否可用,然后發布到服務器上.
在Podfile
中指定pod
,可以指定路徑或者文件
platform :ios, '7.0'
pod 'PODNAME', :path => '項目路徑' # 指定路徑
pod 'PODNAME', :podspec => 'podspec文件路徑' # 指定podspec文件
由于還未將pod
提交到spec repo
中,因此pod install
后的庫文件仍然在Development Pods
目錄下
pod私有庫發布
pod
發布只需要在podspec文件下執行一行命令,此命令會自動將文件push到git倉庫中:
#pod repo push REPONAME [NAME.podspec]
pod repo push MySpecRepo podDemo.podspec
如果想要看詳情可以加上選項--verbose
。一般報錯就是tag
沒打好,提取不到tag代碼,無法驗證podspec也就無法push。
發布到CocoaPods
注冊帳號
#pod trunk register YOUREMAIL 'NAME' --description='DESCRIPTION'
pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook air'
將用戶名,郵箱換成自己的,給設備添加描述信息。由于CocoaPods沒有用戶名密碼,使用的是驗證令牌,所以換一臺電腦的話還需要執行以上命令。
執行成功后會提示以下信息,點擊連接完成注冊。
[!] Please verify the session by clicking the link in the verification email that has been sent to 『your email』
完成后可以執行命令pod trunk me
查看自己賬戶信息。
發布pod
發布到公有庫也只需要一行命令:
pod trunk push [NAME.podspec]
之后也會驗證podspec,通過后上傳。提交到公有庫速度比較慢,需要耐心等待。