一、創建Pod項目工程文件并推送到遠端倉庫
如果有現有的組件項目,并且在Git的版本管理下,那么這一步就算完成了,可以直接進行下一步了。
如果你的組件還在冗余龐大的項目中,需要拆分出來或者需要自己從零開始創建一個組件庫,那么建議使用Cocoapods提供的一個工具來做。
1.1、Using Pod Lib Create 的使用:
這里使用ZSMenuView為例子具體講一下過程,
- 1、創建項目
先cd到要創建項目的目錄然后執行
$ pod lib create ZSMenuView
之后他會問你幾個問題,
1. What is your name?
2. What is your email?
3. What language do you want to use?? [ Swift / ObjC ]
4. Would you like to include a demo application with your library? [ Yes / No ]
5. Which testing frameworks will you use? [ Specta / Kiwi / None ]
6. Would you like to do view based testing? [ Yes / No ]
7. What is your class prefix?
問題的具體介紹可以去看官方文檔
依次回答完這些問題后會自動執行pod install命令創建項目并生成依賴。
-
2、 向Pod文件夾中添加庫文件和資源
把
ZSMenuView
組件所有的代碼文件放入到"ZSMenuView/Classes"中,然后進入"ZSMenuView /Example"文件夾執行pod update
命令,再打開項目工程可以看到,剛剛添加的組件已經在Pods子工程下"Development Pods/ZSMenuView"中了,然后編輯調試demo工程,測試組件。注意1:如果引入不了頭文件,需要在"Build Settings -> User Header Search Paths"中添加
$(PODS_ROOT)設置為recursive
注意2:這里需要注意的是每當你向Pod中添加了新的文件或者以后更新了podspec的版本都需要重新執行一遍pod update命令。 3、將該項目添加并推送到遠端倉庫
$ git add .
$ git commit -m "init commit of ZSMenuView"
$ git remote add origin git@github.com:safiriGitHub/ZSMenuView.git
$ git push -u origin master
#因為podspec文件中獲取Git版本控制的項目還需要tag號,所以我們要打上一個tag
$ git tag -m "first release" "0.1.0"
$ git push --tags
- 4、編輯podspec文件,沒有涉及到的字段可以去官方文檔查閱
#
# Be sure to run `pod lib lint ZSMenuView.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 http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'ZSMenuView' #名稱
s.version = '0.1.0' #版本號
s.summary = 'a simple menuView,every menu can be clicked' #簡短介紹
# 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
TODO: Add long description of the pod here.
小功能:菜單欄。+ 練習使用Cocoapods創建公有podspec
DESC
s.homepage = 'https://github.com/safiriGitHub/ZSMenuView' #主頁,這里要填寫可以訪問到的地址,不然驗證不通過
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' #截圖
s.license = { :type => 'MIT', :file => 'LICENSE' } #開源協議
s.author = { 'safiri' => 'safiri@163.com' } #作者信息
s.source = { :git => 'https://github.com/safiriGitHub/ZSMenuView.git', :tag => '0.1.0' } #項目地址,這里不支持ssh的地址,驗證不通過,只支持HTTP和HTTPS,最好使用HTTPS
s.social_media_url = 'http://www.lxweimin.com/users/2809c84474f6/latest_articles' #多媒體介紹地址
s.ios.deployment_target = '8.0' #支持的平臺及版本
s.source_files = 'ZSMenuView/Classes/**/*' #代碼源文件地址,**/*表示Classes目錄及其子目錄下所有文件,如果有多個目錄下則用逗號分開,如果需要在項目中分組顯示,這里也要做相應的設置
# s.resource_bundles = {
# 'ZSMenuView' => ['ZSMenuView/Assets/*.png']
# } #資源文件地址
# s.public_header_files = 'Pod/Classes/**/*.h' #公開頭文件地址
# s.frameworks = 'UIKit', 'MapKit' #所需的framework,多個用逗號隔開
# s.dependency 'AFNetworking', '~> 2.3' #依賴關系,該項目所依賴的其他庫,如果有多個需要填寫多個s.dependency
end
編輯完podspec文件后,需要驗證一下這個文件是否可用,如果有任何WARNING或者ERROR都是不可以的,它就不能被添加到Spec Repo中。
$ pod lib lint
當你看到
-> ZSMenuView (0.1.0)
ZSMenuView passed validation.
說明驗證通過了
1.2、創建podspec文件
如果已經有了現成的項目,那么就需要給這個項目創建一個podspec文件。創建它需要執行Cocoapods的另外一個命令,官方文檔
$ pod spec create [NAME|https://github.com/USER/REPO]
#Creates a PodSpec, in the current working dir, called NAME.podspec.
編輯完成之后使用驗證命令驗證一下
$ pod lib lint 本地驗證
$ pod spec lint 建議使用遠程驗證
在遠程驗證前要將本地所有改動提交到遠程倉庫,并打好tag標記,在podspec文件中將tag標記改正確。
驗證無誤就可以進入下一步了。
二、本地測試podspec文件
創建一個新的項目,在這個項目的Podfile文件中直接指定剛才創建編輯好的podspec文件,看是否可用。
//在新項目目錄下創建Podfile
$ pod init
//在Podfile中我們可以這樣編輯,有兩種方式,任選其一:
platform :ios, ‘8.0’
target 'podsoecTest' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for podsoecTest
pod ‘ZSMenuView’, :path => ‘/Users/safiri/Downloads/github/ZSMenuView’ #指定路徑
pod ‘ZSMenuView’, :podspec => ‘/Users/safiri/Downloads/github/ZSMenuView/ZSMenuView.podspec’#指定podspec文件
end
執行pod install命令安裝依賴,打開項目工程,可以看到庫文件都被加載到Pods子項目中了,不過它們并沒有在Pods目錄下,而是跟測試項目一樣存在于"Development Pods/ZSMenuView"中,這是因為我們是在本地測試,而沒有把podspec文件添加到Spec Repo中的緣故。
三、向Spec Repo提交podspec,公共或私有(CocoaPods Trunk)
問題:什么是Spec Repo?
它是所有的Pods的一個索引,就是一個容器,所有公開的Pods都在這個里面,是一個Git倉庫,remote端在GitHub上。
條件:
podspec必須通過驗證無誤
刪掉無用的注釋(這個不是必須的,為了規范還是刪掉吧)
步驟:
//Getting started 第一次使用時注冊一個郵箱賬號
$ pod trunk register safiri@163.com 'safiri' --description='first
trunk'
訪問郵箱中的鏈接,注冊成功。
//Deploying a library
$pod trunk push [NAME.podspec] #部署公共的Podspec
$pod repo push REPO [NAME.podspec] #部署私有的Podspec
#例子,pod trunk push ZSMenuView.podspec --allow-warnings (--allow-warnings忽略Xcode的警告)
//Adding other people as contributors
$ pod trunk add-owner ARAnalytics kyle@cocoapods.org
#For example, to add kyle@cocoapods.org to the library ARAnalytics
四、使用制作好的Pod
在項目中
$ pod 'ZSMenuView', '~> 0.1.0'
然后執行pod update,更新庫依賴,然后打卡項目可以看到,我們自己的庫文件已經出現在Pods子項目中的Pods子目錄下了,而不再是Development Pods。
五、更新維護podspec
升級代碼 -> 根據具體改動,編輯name.podspec -> pod lib lint驗證 -> 將項目推送到遠端倉庫,打上新的tag(git tag '0.2.0'
git push --tags
) -> 提交到Spec Repo中,$pod trunk push [NAME.podspec] (pod trunk push ZSMenuView.podspec
)
遇到的問題:
一
sudo gem update --system
升級gem ->sudo gem install -n /usr/local/bin cocoapods --pre
升級cocoapods ->pod setup
安裝cocoapods->pod lib lint
驗證
history
查看終端歷史記錄
pod search
查詢不到最新版本 ---> pod repo update
更新本地緩存
如果還是不行:
嘗試刪除本地緩存,重新setup,
$rm -fr ~/.cocoapods/repos/master
$pod setup
二、pod lib lint
執行時報錯
- ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
- ERROR | xcodebuild: aaa.h:10:9: error: include of non-modular header inside framework module 'xx': 'bbb.h' [-Werror,-Wnon-modular-include-in-framework-module]
原因:某個頭文件(aaa.h)中直接import了依賴的第三方庫的頭文件(bbb.h),采用了向前聲明@Class
解決。
三、刪除提交到cocoapods上的框架?
查看CocoaPods上的個人信息:
pod trunk me
有時候如果提交有問題,可以執行一下命令刪除:
// 直接廢去這個pod
pod trunk deprecate xxx
// 廢去這個pod的某個版本
pod trunk delete xxx 1.0.0
經測試,兩個命令使用完都沒返回成功,且pod search是可以找到的,可能是本地庫的原因。然后更新本地庫pod repo update
后,版本會刪除成功。