如果您能看這篇文章,相信您已經(jīng)了解并喜歡上了 Cocoapods, 下面我們一步一步的創(chuàng)建一個屬于自己的 Cocoapods 庫.
創(chuàng)建
通過下面命令,可以使用一個創(chuàng)建向?qū)Ш啔v Cocoapods 庫
pod lib create GUICodeSnippet
然后終端會進(jìn)行引導(dǎo),依次選擇即可. 回車鍵確認(rèn)默認(rèn)選擇.
問題依次是:
語言
創(chuàng)建demo工程( 極力推薦創(chuàng)建)
選擇測試框架 (可選擇默認(rèn),如果非常確定沒有需要,可以選擇None)
View-based Testing (官方推薦使用
FBSnapShotTestCase
)OC類前綴 (如果語言選擇非OC,不會有這項)
打開工程,會發(fā)現(xiàn)有一個 GUICodeSnippet 和一個 Pods, 在
Pods->Development Pods->GUICodeSnippet->Pod->Classes
中,有一個ReplaceMe.m
你需要將這個文件替換成自己的代碼文件.
源碼驗證
進(jìn)入 Example 目錄 , 執(zhí)行
pod install
然后 Xcode 打開 xcworkspace 文件 ,去跑一下項目,如果沒有錯誤,證明添加的代碼沒有問題,可以繼續(xù)進(jìn)行
準(zhǔn)備工作
配置 podspec 文件
#
# Be sure to run `pod lib lint GUICodeSnippet.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 = "GUICodeSnippet"
s.version = "0.1.8"
s.summary = "code segment of kennyGui"
# 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
code segment of kenny ,and in most situation ,is for private use only
DESC
s.homepage = "https://git.oschina.net/kennygui/GUICodeSnippet"
# s.screenshots = "www.example.com/screenshots_1", "www.example.com/screenshots_2"
s.license = 'MIT'
s.author = { "katsurake" => "katsurake@foxmail.com" }
s.source = { :git => "https://git.oschina.net/kennygui/GUICodeSnippet.git", :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.platform = :ios, '7.0'
s.requires_arc = true
s.source_files = 'Pod/Classes/*.h'
s.resource_bundles = {
'GUICodeSnippet' => ['Pod/Assets/*.png']
}
s.public_header_files = 'Pod/Classes/*.h'
s.frameworks = 'UIKit', 'MapKit'
s.dependency 'AFNetworking', '~> 2.3'
s.dependency 'CocoaLumberjack', '~> 2.2.0'
s.dependency 'MBProgressHUD', '~> 0.9.2'
s.dependency 'Masonry', '~> 0.6.4'
s.dependency 'ReactiveCocoa', '~> 2.5'
s.dependency 'YapDatabase', '~> 2.8.3'
s.dependency 'DTCoreText', '~> 1.6.17'
s.dependency 'MJExtension', '~> 3.0.10'
end
創(chuàng)建完畢之后,隨手使用
pod lib lint
命令驗證是否編寫正確,如果沒有正確,根據(jù)提示修改即可.
幾個好用的命令:
--verbose
完整打印詳情,可以幫助我們調(diào)試,用法 pod lib lint --verbose
--allow-warnings
忽略警告,由于一些三方代碼有警告,所以造成始終無法通過,可以用這個方法忽略警告,用法同上
創(chuàng)建代碼倉庫
在github 或者其他托管平臺建立一個空項目,最好項目名稱和本地的一致
git add .
git commit -m “Initial Commit"
git remote add origin https://github.com/kennyGui/GUICodeSnippet.git
git push -u origin master
其實我更喜歡的方式是, Clone 空項目,然后替換代碼的 .git 文件夾,這樣更暴力,具體怎么選擇,看個人喜好.
打 tag
git tag -a 0.1.0 -m "初始版本"
通常的git push不會將標(biāo)簽對象提交到git服務(wù)器,我們需要進(jìn)行顯式的操作:
git push origin 0.1.2 # 將v0.1.2標(biāo)簽提交到git服務(wù)器
或者
git push origin –tags # 將本地所有標(biāo)簽一次性提交到git服務(wù)器
驗證標(biāo)簽成功
git tag #查看標(biāo)簽
推送描述文件
使用這個命令推送自己的庫的信息到官方的 Master 庫
pod trunk push GUICodeSnippet.podspec
但是如果你是第一次使用的話,需要注冊 Session
使用如下命令注冊自己的郵箱(注意替換成自己的郵箱)
pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook air'
稍后, 郵箱會收到郵件, 點擊鏈接即可
pod trunk push BlinkingLabel.podspec
由于網(wǎng)絡(luò)原因(你懂得),可能會推送失敗,多嘗試幾次即可.
驗證
可以讓同事或者朋友使用
pod search GUICodeSnippet
這個命令進(jìn)行測試,但是,如果是第一次,應(yīng)該是找不到的.
需要使用
pod setup
更新一下本地的 Maser 庫信息
結(jié)語
好了,現(xiàn)在,你就可以將自己的代碼共享給小伙伴使用啦.
現(xiàn)在 pod 里面的 Class 都是放在一個文件夾下的,沒有辦法實現(xiàn)子文件夾,(即使工程里面有多個文件夾,但是弄好 cocoapods 之后,也是都放到一個文件夾下面了)
這需要我們的 spec 里面配置 subspec ,這個知識點會在下一篇文章中說
如果你遇到了問題,請留言,我看到后會盡力解答,共同進(jìn)步!