前言
iOS開發(fā)者取试,會經(jīng)常使用CocoaPods管理第三方庫膳灶。但是,無論是組件化開發(fā)還是需要創(chuàng)建公司內(nèi)部的類庫球订,我們都可以制作自己私有的CocoaPods庫。本文就是詳細說明私有庫創(chuàng)建流程瑰钮。
1.準備工作
- 創(chuàng)建兩個私有遠程倉庫 LWSpec 和 LWLib
-
WYSpec
倉庫用來存儲本地spec
-
WYLib
用來存儲項目工程文件
-
2.創(chuàng)建pod私有庫的項目工程
cd
到合適目錄下用命令行創(chuàng)建工程-
執(zhí)行
pod lib create WYLib
MacBook-Pro ~ % cd desktop MacBook-Pro desktop % pod lib create LWLib
-
回車之后終端會進行靈魂冒滩,按照自己的需求回答。
#選擇平臺 What platform do you want to use?? [ iOS / macOS ] > iOS #選擇編程語言 What language do you want to use?? [ Swift / ObjC ] > Objc #在你的項目中是否創(chuàng)建一個demo工程浪谴,為了方便測試开睡,我選擇了Yes Would you like to include a demo application with your library? [ Yes / No ] > Yes #測試框架選擇哪一個 Which testing frameworks will you use? [ Specta / Kiwi / None ] > None #要不要做視圖測試 Would you like to do view based testing? [ Yes / No ] > Yes # 類前綴名 What is your class prefix? > LW security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain. security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain. security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain. security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain. security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain. security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain. Running pod install on your new library. Analyzing dependencies Fetching podspec for `YOURPODNAME` from `../` Downloading dependencies Installing YOURPODNAME (0.1.0) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `LWLib.xcworkspace` for this project from now on. Sending stats Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed. Ace! you're ready to go! We will start you off by opening your project in Xcode open 'LWLib/Example/LWLib.xcworkspace' To learn more about the template see `https://github.com/CocoaPods/pod-template.git`. To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
創(chuàng)建完成后自動打開
然后進入
LWLib
文件夾中找到Classes
中的ReplaceMe.m
文件刪除,然后將自己所需要的文件放到這里目錄下cd
到Example
路徑下苟耻,執(zhí)行pod install
更新Example
中的pod
-
打開
Example
中的.workspace
文件篇恒,打開工程。找到
. podspec
-
修改
. podspec
Pod::Spec.new do |s| #項目名稱 s.name = 'LWLib' #版本號 s.version = '0.1.0' #項目摘要 s.summary = 'LWLib.' # 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 LWLib的項目描述婚度,這是自己創(chuàng)建的一個項目 DESC #主頁,這里要填寫可以訪問的地址官卡,不然驗證不通過 #個人遠程倉庫的地址 s.homepage = 'https://gitee.com/ZhaoLaoWu/LWLib' # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' s.license = { :type => 'MIT', :file => 'LICENSE' } #作者信息 s.author = { '趙桂安' => '623916490@qq.com' } #項目地址蝗茁,這里不建議使用ssh地址,會有警告寻咒;建議使用HTTP或者HTTPS哮翘,最好使用HTTPS。 #更改成自己的遠程倉庫地址 s.source = { :git => 'https://gitee.com/ZhaoLaoWu/LWLib.git', :tag => s.version.to_s } # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>' #最低支持的iOS版本 s.ios.deployment_target = '8.0' #代碼源文件地址 s.source_files = 'LWLib/Classes/**/*' # s.resource_bundles = { # 'LWLib' => ['LWLib/Assets/*.png'] # } # s.public_header_files = 'Pod/Classes/**/*.h' #根據(jù)自己是否有依賴庫來決定是否添加 #依賴的frameworks # s.frameworks = 'UIKit', 'MapKit' #依賴的公共庫或者私有庫 # s.dependency 'AFNetworking', '~> 2.3' end
-
修改完成后,
cd
到LWLib
文件執(zhí)行
pod lib lint
出一下表示成功
MacBook-Pro LWLib % pod lib lint -> LWLib (0.1.0) #注意如果這有警告毛秘,最好把問題解決了饭寺,我這個是已經(jīng)解決的。 - NOTE | xcodebuild: note: Using new build system - NOTE | xcodebuild: note: Building targets in parallel - NOTE | [iOS] xcodebuild: note: Planning build - NOTE | [iOS] xcodebuild: note: Constructing build description - NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App') - NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'LWLib' from project 'Pods') - NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods') - NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App') LWLib passed validation. #已經(jīng)完成通過
3.將本地文件上傳到遠程倉庫
MacBook-Pro LWLib % git remote add origin https://gitee.com/ZhaoLaoWu/LWLib.git
MacBook-Pro LWLib % git add .
MacBook-Pro LWLib % git commit -m "Initial commit"
MacBook-Pro LWLib % git push -u origin master
#To https://gitee.com/ZhaoLaoWu/LWLib.git
# ! [rejected] master -> master (fetch first)
#error: failed to push some refs to 'https://gitee.com/ZhaoLaoWu/LWLib.git'
#hint: Updates were rejected because the remote contains work that you do
#hint: not have locally. This is usually caused by another repository pushing
#hint: to the same ref. You may want to first integrate the remote changes
#hint: (e.g., 'git pull ...') before pushing again.
#hint: See the 'Note about fast-forwards' in 'git push --help' for details.
#注意:當(dāng)執(zhí)行推送遠端失敗時叫挟,先執(zhí)行以下操作
MacBook-Pro LWLib % git pull origin master
#From https://gitee.com/ZhaoLaoWu/LWLib
# * branch master -> FETCH_HEAD
#fatal: refusing to merge unrelated histories
#注意:如果出現(xiàn)上面所示錯誤艰匙,需添加--allow-unrelated-histories即可
MacBook-Pro LWLib % git pull origin master --allow-unrelated-histories
#From https://gitee.com/ZhaoLaoWu/LWLib
# * branch master -> FETCH_HEAD
#CONFLICT (add/add): Merge conflict in README.md
#Auto-merging README.md
#Automatic merge failed; fix conflicts and then commit the result.
# 合并README.md的沖突 然后 commit
MacBook-Pro LWLib % git add .
MacBook-Pro LWLib % git commit -m '解決合并沖突'
# 推送更新到遠程倉庫
#注意:第一次推送master分支時,加上了-u參數(shù)抹恳,Git不但會把本地的master分支內(nèi)容推送的遠程新的master分支员凝,還會把本地的master分支和遠程的master分支關(guān)聯(lián)起來,后續(xù)推送不需要再使用-u參數(shù)
MacBook-Pro LWLib % git push -u origin master
#Enumerating objects: 116, done.
#Counting objects: 100% (116/116), done.
#Delta compression using up to 4 threads
#Compressing objects: 100% (104/104), done.
#Writing objects: 100% (114/114), 53.17 KiB | 3.54 MiB/s, done.
#Total 114 (delta 25), reused 0 (delta 0)
#remote: Resolving deltas: 100% (25/25), completed with 1 local object.
#remote: Powered by GITEE.COM [GNK-5.0]
#To https://gitee.com/ZhaoLaoWu/LWLib.git
# 82f9eaf..b38a835 master -> master
#Branch 'master' set up to track remote branch 'master' from 'origin'.
#tag 值要和podspec中的version一致
MacBook-Pro LWLib % git tag 0.1.0
#推送tag到服務(wù)器上
MacBook-Pro LWLib % git push --tags
#Total 0 (delta 0), reused 0 (delta 0)
#remote: Powered by GITEE.COM [GNK-5.0]
#To https://gitee.com/ZhaoLaoWu/LWLib.git
# * [new tag] 0.1.0 -> 0.1.0
這是查看遠程倉庫已經(jīng)有代碼了
4.校驗Spec
-
執(zhí)行
pod spec lint
zhaoguian@zhaoguiandeMacBook-Pro LWLib % pod spec lint -> LWLib (0.1.0) - NOTE | xcodebuild: note: Using new build system - NOTE | xcodebuild: note: Building targets in parallel - NOTE | [iOS] xcodebuild: note: Planning build - NOTE | [iOS] xcodebuild: note: Constructing build description - NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App') - NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'LWLib' from project 'Pods') - NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods') - NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App') Analyzed 1 podspec. LWLib.podspec passed validation.
出現(xiàn)上述描述表示成功奋献。
5.創(chuàng)建spec repo
進入文件夾
~/.cocoapods/repos
可以查看本地spec repo
指定管理
lib repo
的specs repo
的url
pod repo add LWLib https://gitee.com/ZhaoLaoWu/LWSpec.git
注意 此處地址是spec倉庫地址-
推送
podspec
到specs repo
pod repo push LWLib LWLib.podspec --sources=https://gitee.com/ZhaoLaoWu/LWSpec.git
MacBook-Pro LWLib % pod repo push LWLib LWLib.podspec --sources=https://gitee.com/ZhaoLaoWu/LWSpec.git Validating spec -> LWLib (0.1.0) - NOTE | xcodebuild: note: Using new build system - NOTE | xcodebuild: note: Building targets in parallel - NOTE | [iOS] xcodebuild: note: Planning build - NOTE | [iOS] xcodebuild: note: Constructing build description - NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App') - NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'LWLib' from project 'Pods') - NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods') - NOTE | [iOS] xcodebuild: note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App') Updating the `LWLib' repo Adding the spec to the `LWLib' repo - [Add] LWLib (0.1.0) Pushing the `LWLib' repo
出現(xiàn)上述表示已經(jīng)成功
6.驗證
- 創(chuàng)建一個新項目健霹,在
Podfile
中創(chuàng)建并使用
source ‘https://gitee.com/ZhaoLaoWu/LWSpec.git’
platform:ios, '10.0'
target 'CSLWLib' do
use_frameworks!
pod 'LWLib'
end
-
cd
項目中 執(zhí)行pod install
MacBook-Pro % ~ % cd /Users/zhaoguian/Desktop/CSLWLib
MacBook-Pro CSLWLib % pod install
Analyzing dependencies
Downloading dependencies
Installing LWLib (0.1.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
[!] Smart quotes were detected and ignored in your Podfile. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.
zhaoguian@localhost CSLWLib %
完成