? ?首先本人會以github為例,創(chuàng)建自己的私有庫。不懂請看下面,讓我慢慢告訴你怎么創(chuàng)建自己的公有庫!
第一步 用本人賬號在github創(chuàng)建一個公有倉庫

MIT License 是稍后podspec里面要用到的協(xié)議
第二步?Cocoapods注冊Trunk

$pod trunk register [Your-Email] '[Your-Name]' --description='[Your-Desc]'
> [Your-Email]: 任意郵件,但是我比較推薦你使用github上的Email
> [Your-Name]: 推薦使用github上使用的Name
> [Your-Desc]: 一個簡單的描述,往往這個時候我們使用的是自己電腦的一個描述
// 比如我自己注冊了一個$?pod trunk register 1136200379@qq.com 'PCZheng' --description='iMac (Retina 4K, 21.5-inch, 2017)'// 注冊完成后,你可以通過[pod trunk me]查看信息
$ pod trunk me ?查看信息是會有[!] Authentication token is invalid or unverified. Either verify it with the email that was sent or register a new session.這是要在你注冊的[Your-Email]查看給你發(fā)的email點擊里面的鏈接認(rèn)證。由于我沒有認(rèn)證所以,pod trunk me 并能查看信息,需要叫我認(rèn)證,由于最先沒反應(yīng)過來,我又注冊了trunk一次,注冊錯了可以刪除,刪除具體的方法執(zhí)行pod trunk --help,查看刪除方法。
第三步、將你第一步創(chuàng)建的庫,clone到指定的文件夾里面


第四步、創(chuàng)建.podspec文件 并且編輯.podspec
該步驟是重點也是難點,該步驟要是出什么錯誤了,后面都是扯淡,本人就因為各種各樣的問題在這個步驟卡了很久,最后通過各種解決方案解決了,有些坑只有自己趟過了才知道難不難,所以廢話也不多說,遇到各種不會的就goole和百度吧,或者@我也行。
.podspec 即pod描述文件podspec specification?
為了講明白這一步,這一步主要分為下面這幾個步驟:
1.如何創(chuàng)建podspec文件
2.如何編寫podspec文件
3.如何實現(xiàn)目錄分層
4.如何校驗podspec文件
5.在校驗podspec文件所遇到過的問題
1.如何創(chuàng)建podspec文件
$pod spec create 庫名
例如我自己的PageController


該目錄下就會多一個PageController.podspec文件,可以有文本編輯打開對這個文件進(jìn)行編輯

Pod::Spec.new do |s|
? s.name? ? ? ? = "PageController" #框架名
? s.version? ? ? = "0.0.1" #當(dāng)前版本號,在pod search PageController的時候會顯示該信息
? s.summary? ? ? = "A short description of PageController."#詳細(xì)描述,在pod search PageController的時候會顯示介紹
? s.description? = <<-DESC
? ? ? ? ? ? ? ? ? DESC
? s.homepage? ? = "http://EXAMPLE/PageController"#頁面鏈接
? # s.screenshots? = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
? s.license? ? ? = "MIT (example)"#開源協(xié)議,也就是第一步選擇的協(xié)議
? # s.license? ? ? = { :type => "MIT", :file => "FILE_LICENSE" }
? s.author? ? ? ? ? ? = { "RainbowWait" => "1136200379@qq.com" }#作者
? # Or just: s.author? ? = "RainbowWait"
? # s.authors? ? ? ? ? ? = { "RainbowWait" => "1136200379@qq.com" }
? # s.social_media_url? = "http://twitter.com/RainbowWait"
? #s.platform? ? = :ios
? s.platform? ? = :ios, "8.0"#支持iOS平臺最低版本
? #? When using multiple platforms
? # s.ios.deployment_target = "5.0"
? # s.osx.deployment_target = "10.7"
? # s.watchos.deployment_target = "2.0"
? # s.tvos.deployment_target = "9.0"
? s.source? ? ? = { :git => "http://EXAMPLE/PageController.git", :tag => "#{s.version}" }#源碼git地址 以及tag標(biāo)簽
? s.source_files? = "Classes", "Classes/**/*.{h,m}"#源文件(可以包含.h和.m)
? s.exclude_files = "Classes/Exclude"
? # s.public_header_files = "Classes/**/*.h"#頭文件(.h文件)
? # s.resource? = "icon.png"
? # s.resources = "Resources/*.png"#資源文件(配置的文件都會放到mainBundle中)
? # s.preserve_paths = "FilesToSave", "MoreFilesToSave"
? # s.framework? = "SomeFramework"
? # s.frameworks = "SomeFramework", "AnotherFramework"#依賴的系統(tǒng)的框架
? # s.library? = "iconv"
? # s.libraries = "iconv", "xml2"#依賴的系統(tǒng)庫
? s.requires_arc = true#支持ARC
? # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
? # s.dependency "JSONKit", "~> 1.4"#依賴的第三方庫
https://guides.cocoapods.org/syntax/podspec.html? 請參照Podfile詳細(xì)
上面列舉的信息大部分來自pod默認(rèn)生成的podspec文件中的給的,其中需要重點注意標(biāo)識黑體的這幾個信息
下面我會舉例說明giant如何寫podspec中的每項配置。


s.source?源代碼地址 以及tag 標(biāo)簽
s.source???????= { :git => "https://github.com/RainbowWait/PageController.git", :tag => "0.0.1" }
git tag 0.0.1?? 設(shè)置的tag標(biāo)簽為0.0.1??git tag -d 0.0.1刪除0.0.1的tag標(biāo)簽 最后提交的時候git push --tags
s.source_files?配置三方庫的源文件(.h或.m文件),swift是配置.swift文件
寫法:
OC:s.source_files = "Classes/**/*.{h,m}"#Classes路徑下的所有匹配文件
???????s.source_files = "Classes/*.{h,m}"#Classes文件夾下的所有匹配文件
???????s.source_files = “Classes/PageController.{h,m}"#直接指定文件名
Swift:s.source_files = "Classes/**/*.{swift}"#Classes路徑下的所有匹配文件
???????????s.source_files = "Classes/*.{swift}"#Classes文件夾下的所有匹配文件
???????????s.source_files = “Classes/PageController.{swift}"#直接指定文件名
public_header_files頭文件
s.public_header_files = "Classes/**/*.h"#Classes路徑下所匹配的.h文件
s.public_header_files = "Classes/*.h"
s.public_header_files = "Classes/Public.h"
vendored_frameworks?引用非系統(tǒng)的框架
s.ios.vendored_frameworks = "Frameworks/MyFramework.framework"
s.vendored_frameworks = 'MyFramework.framework’,'TheirFramework.framework'
frameworks配置系統(tǒng)框架
s.frameworks = 'Foundation', 'CoreGraphics', 'UIKit'
vendored_libraries?非系統(tǒng)靜態(tài)庫(要注意,這里的.a靜態(tài)庫名字必須要帶lib前綴,如果引用的靜態(tài)庫名字沒lib前綴會導(dǎo)致編譯報錯,只需要重命名加上即可)
s.ios.vendored_library = 'Libraries/libProj4.a'
s.vendored_libraries = 'libProj4.a', 'libJavaScriptCore.a'
libraries:配置依賴的系統(tǒng)庫(要注意,這里的寫法需要忽略lib前綴)
libraries = 'c++’,'sqlite3', 'stdc++.6.0.9', 'z'
resources:配置資源文件(.bundle,.png,.txt等資源文件,這些資源文件會被放到mainBundle中,要注意避免發(fā)生命名重復(fù)的問題)
s.resources = ['Images/*.png’,'Sounds/*']
s.resource = 'Resources/HockeySDK.bundle'
resource_bundles:配置指定bundle的資源文件(可以解決resources導(dǎo)致的命名沖突問題)
spec.ios.resource_bundle = { 'MapBox' => 'MapView/Map/Resources/*.png' }
spec.resource_bundles = {
????'MapBox' => ['MapView/Map/Resources/*.png'],
????'OtherResources' => ['MapView/Map/OtherResources/*.png']
??}
swift_version?支持Swift的版本
s.swift_version = '4.0'
dependency:依賴的三方庫,pod庫或者可以是自身的subspec
3.如何實現(xiàn)目錄分層3.如何實現(xiàn)目錄分層
使用subspec來實現(xiàn)目錄分層。
目錄分層的好處:
目錄分層,結(jié)構(gòu)清晰;
使用pod引入一個三方庫時,可以只引入一個subspec而不用將整個三方庫引入。
例如AFNetworking:

嵌套請參考?CocoaPods Guides - Podspec Syntax Reference v1.6.0.beta.2
4.如何校驗podspec文件
pod?lib lint (首先從本地驗證你的pod能否通過驗證)
pod?spec lint (從本地和遠(yuǎn)程驗證你的pod能否通過驗證)
pod?lib lint --verbose (加--verbose可以顯示詳細(xì)的檢測過程,出錯時會顯示詳細(xì)的錯誤信息)
pod?lib lint --allow-warnings (允許警告,用來解決由于代碼中存在警告導(dǎo)致不能通過校驗的問題)
pod?lib lint --help (查看所有可選參數(shù),可選參數(shù)可以加多個)
首先把你的代碼先提交到遠(yuǎn)程倉庫,并且把設(shè)置的標(biāo)簽頁推送到遠(yuǎn)程倉庫,以下是截圖




修改后的PageController.podspec文件

從本地和遠(yuǎn)程驗證pod能否驗證通過
pod spec lint --allow-warnings

第五步、搭建私有庫環(huán)境
1.創(chuàng)建自己的私有庫
pod repo add ?[repo名] ?[repo Git地址]
例如:pod repo add PageControllerRepo https://github.com/RainbowWait/PageController.git
查看私有庫是否創(chuàng)建成功
pod repo list 下面的lists錯誤了是list

需要注意的是:
?1.?要記得將代碼提交到遠(yuǎn)端
2.?要記得打tag,每個tag對應(yīng)一個三方庫版本
3.?podspec文件中version的值要與git中的一個tag對應(yīng)
2.向私有的repo庫中提交podspec
pod repo push [repo名] [.podspec文件名] 在驗證的時候有警告就要加上--allow-warnings,否則會失敗

3.提交公有l(wèi)ibrary
pod trunk push [NAME].podspec
pod repo update// 查找一下你提交的pod
pod search PageController
文章參考了?Cocoapods整理(四)——搭建Cocoapods私有庫環(huán)境 - Zcp大官人的iOS開發(fā)專欄 - SegmentFault 思否
Cocoapods整理(三)——編寫podspec文件 - Zcp大官人的iOS開發(fā)專欄 - SegmentFault 思否
