創(chuàng)建GIT倉(cāng)庫(kù)
Repository name 倉(cāng)庫(kù)名稱
Description 描述
Public / Private 公開(kāi)倉(cāng)庫(kù)或者私人倉(cāng)庫(kù)
Initialize this repository with a README 創(chuàng)建一個(gè)README文件
Add .gitignore 需要忽略的文件類(lèi)型
Add a license 添加一個(gè)許可證塑荒,如果項(xiàng)目需要上傳到cocoapods铁材,最好選擇一個(gè)碗暗。
將倉(cāng)庫(kù)克隆到本地指定目錄探熔,該目錄下將自動(dòng)創(chuàng)建HCKit-Swift文件夾,并且包含LICENSE和README.md文件殖告。
cd /Users/chenhc/Documents/Swift
git clone https://github.com/chyo/HCKit-Swift.git
創(chuàng)建一個(gè)項(xiàng)目阿蝶,并將項(xiàng)目根目錄中的文件移動(dòng)到HCKit-Swift目錄中
創(chuàng)建Pods依賴文件
cd HCKit-Swift
pod spec create HCKit-Swift
HCKit-Swift根目錄中將出現(xiàn)HCKit-Swift.podspec文件,將文件的內(nèi)容修改為自己所需的
Pod::Spec.new do |s|
# 項(xiàng)目名
s.name = "HCKit-Swift"
# 版本號(hào)
s.version = "0.0.1"
# 摘要
s.summary = "一個(gè)簡(jiǎn)單的Swift庫(kù)"
# 描述
s.description = <<-DESC
一個(gè)森羅萬(wàn)象的Swift庫(kù)
DESC
# 主頁(yè)
s.homepage = "https://github.com/chyo/HCKit-Swift"
# 證書(shū)
s.license = "MIT"
# 作者
s.author = { "ChenHongchao" => "xmchc@hotmail.com" }
# 平臺(tái)
s.platform = :ios, "8.0"
# Swift版本
s.swift_version = "4.0"
# 來(lái)源
s.source = { :git => "https://github.com/chyo/HCKit-Swift.git", :tag => "#{s.version}" }
# 源代碼
s.source_files = "Classes/*.swift"
# 資源文件
# s.resources = "Resources/*.png"
# 依賴系統(tǒng)庫(kù)
# s.framework = "SomeFramework"
# s.frameworks = "SomeFramework", "AnotherFramework"
# s.library = "iconv"
# s.libraries = "iconv", "xml2"
# ARC
s.requires_arc = true
# XC配置
# s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
# 依賴庫(kù)
# s.dependency "JSONKit", "~> 1.4"
end
創(chuàng)建項(xiàng)目
創(chuàng)建一個(gè)項(xiàng)目黄绩,在項(xiàng)目中創(chuàng)建一個(gè)實(shí)體文件夾Classes(非group)羡洁,并在其中新建一個(gè)Swift類(lèi)。
關(guān)閉項(xiàng)目爽丹,將項(xiàng)目根目錄中的文件全部移動(dòng)到HCKit-Swift中筑煮。
項(xiàng)目中的實(shí)體文件夾名稱Classes和剛才所創(chuàng)建的HCKit-Swift.podspec文件中的s.source_files需保持一致,并且如果此文件夾包含多級(jí)目錄粤蝎,層級(jí)關(guān)系也要保持一致真仲。假設(shè)Classes文件夾中有二級(jí)目錄,那么s.source_files就應(yīng)該改為:
s.source_files = "Classes/**/*.swift"
同步GIT倉(cāng)庫(kù)
// 版本標(biāo)簽诽里,要與HCKit-Swift.podspec文件保持一致
git tag 0.0.1
// 上傳標(biāo)簽
git push --tags
// 添加文件
git add .
// 提交
git commit -m '初次提交'
// 上傳袒餐,默認(rèn)合并到master主干
git push
// 校驗(yàn)pod文件
pod spec lint HCKit-Swift.podspec
根據(jù)校驗(yàn)的結(jié)果修改產(chǎn)生的錯(cuò)誤飞蛹,WARN類(lèi)型的警告可以選擇性處理谤狡,其他常見(jiàn)的一些錯(cuò)誤:
Could not find remote branch 0.0.1 to clone.
// 無(wú)法找到版本號(hào)為0.0.1的分支/主干,執(zhí)行 git push --tags 即可
The `source_files` pattern did not match any file.
The `resources` pattern did not match any file.
The `preserve_paths` pattern did not match any file.
module_map: Unable to find the specified module map file.
// 這幾種錯(cuò)誤的處理方式都是一樣的卧檐,主要是podspec中配置的路徑與實(shí)際路徑不一致所導(dǎo)致的墓懂,
// 如果確認(rèn)路徑無(wú)誤,那么需要重新設(shè)定.podspec文件中的s.version霉囚,
// 重新執(zhí)行上述“同步GIT倉(cāng)庫(kù)”的操作捕仔,tag需要與s.version保持一致。
The validator used Swift 3.2 by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_version` attribute in your podspec. Note that usage of the `--swift-version` parameter or a `.swift-version` file is now deprecated.
// 這個(gè)主要是因?yàn)镾wift版本引起的盈罐,需指定代碼運(yùn)行的Swift版本
// 打開(kāi).podspec文件榜跌,添加 s.swift-version = '4.0.0'
如果通過(guò)校驗(yàn),則會(huì)提示
-> HCKit-Swift (0.0.1)
Analyzed 1 podspec.
HCKit-Swift.podspec passed validation.
執(zhí)行l(wèi)int操作后根據(jù)cocoapods的安裝目錄盅粪,可以在本地文件夾中找到代碼庫(kù)
~/Library/Caches/CocoaPods/Pods/External
其他的一些操作(非必須)
// 刪除版本標(biāo)簽
git tag -d 0.0.1
// 拉取代碼
git pull
發(fā)布到Cocoapods
注冊(cè)trunk钓葫,注冊(cè)成功會(huì)在郵箱中收到一個(gè)驗(yàn)證地址,驗(yàn)證成功后就可以查看自己的個(gè)人信息票顾。
pod trunk register 電子郵件 '用戶名' --verbose
// 郵箱驗(yàn)證后础浮,執(zhí)行
pod trunk me
最后直行push操作將庫(kù)上傳到pods中帆调,注意庫(kù)的名稱不能與其他人重復(fù),否則會(huì)提示 xxx庫(kù)的作者是xxxx豆同。
pod trunk push
// 上傳結(jié)果番刊,這個(gè)過(guò)程需要一段時(shí)間
// [!] Found podspec `HCKit-Swift.podspec`
// Updating spec repo `master`
// Validating podspec
// -> HCKit-Swift (0.0.3)
// Updating spec repo `master`
// --------------------------------------------------------------------------------
// ?? Congrats
//
// ?? HCKit-Swift (0.0.3) successfully published
// ?? May 21st, 20:53
// ?? https://cocoapods.org/pods/HCKit-Swift
// ?? Tell your friends!
// --------------------------------------------------------------------------------
此后每次版本變更需要執(zhí)行以下幾個(gè)步驟:
1. 變更.podspec文件的s.version
2. 同步GIT倉(cāng)庫(kù),tag與s.version保持一致
3. trunk push
trunk成功后如果搜索不到自己的庫(kù)影锈,可以重新同步下pods代碼
pod setup
如果setup之后依然搜索不到芹务,那么需要重新建立檢索文件
// 進(jìn)入 /Users/用戶名/Library/Caches/CocoaPods 文件夾刪除search_index.json
// 執(zhí)行
pod search HCKit-Swift
刪除發(fā)布到cocoapods的框架
pod trunk delete 框架名稱 版本號(hào)
CommonCrypto
在Swift中使用CommonCrypto庫(kù)一般有兩種方式,一種是建立橋接文件鸭廷,一種是使用modulemap锄禽。我用的是modulemap的方式,模擬器和真機(jī)編譯一切都正常靴姿,但是校驗(yàn)podsepc的時(shí)候提示了失敗沃但。
// 沒(méi)配置相關(guān)信息的時(shí)候提示
no such module 'CommonCrypto'
// 配置了s.module_map、s.preserve_path佛吓、s.xcconfig的時(shí)候提示
HCKit-Swift/Classes/Extension/String+HCExtension.swift:10:8: error: could not build Objective-C module 'CCommonCrypto'
在stackflow上看了好多方法都不管用宵晚,最后終于找到了方案,記住维雇,podspec中的 s.module_map = "xxxx" 這個(gè)是無(wú)效的淤刃,無(wú)效的!
在根目錄創(chuàng)建文件夾Module吱型,在文件夾中創(chuàng)建空文件(Other/empty)逸贾,命名為module.modulemap,編輯文件:
module CommonCrypto [system] {
header "/usr/include/CommonCrypto/CommonCrypto.h"
export *
}
在項(xiàng)目的Build Settings中找到Import Paths津滞,設(shè)置其值為$(PROJECT_DIR)/Module铝侵,編譯,這時(shí)候就可以正常引用了 import CommonCrypto触徐。
打開(kāi).podspec文件咪鲜,修改source_files、preserve_path撞鹉、xcconfig:
// modulemap要配置在source_files中疟丙,配置在module_map中不起作用
s.source_files = "Classes/**/*.swift", "Module/*.modulemap"
s.preserve_path = "Module/module.modulemap"
s.xcconfig = { "SWIFT_INCLUDE_PATHS" => "$(PODS_ROOT)/HCKit-Swift/Module"}
接下來(lái)就是打版本標(biāo)簽,提交鸟雏,校驗(yàn)即可享郊。