一、CocoaPods 簡單快捷的建立私有庫

語言: CN / TW / HK

前言

網上看了很多的私有庫建立,經過一大堆彎彎繞繞的命令,最後都會存這個問題([!] An unexpected version directory Classes)。所以寫此文章記錄一下。

一、建立遠端倉庫

個大倉庫均可任選其一,本文以碼雲為例

倉庫建立注意事項:

1、建立前,先確認倉庫名稱是否被佔用

pod search XCTest 如上,沒有找到相關的庫,說明該庫沒有被佔用,我們可以使用這個庫名。

2、填寫倉庫名稱和描述即可直接建立倉庫

3、通過工具SourceTree在本地,如果拉取後有分支,可以建立一個main分支,並進入碼雲倉庫管理頁面設定預設分支為main分支。如果不能建立就到第六步在執行也沒有有關係(我這裡沒有選擇分支模版,所以把分支建立放在第六步建立,如果選擇了分支模版到,預設有master分支,可以在這裡直接建立一個main分支)。main分支作用是作為你私有庫到索引,所以要設定為預設分支。

二、建立工程專案

專案名稱與倉庫名稱一致 `` pod lib create XCTest Cloninghttp://github.com/CocoaPods/pod-template.gitintoXCTest`. Configuring project template. security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.


To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: - http://guides.cocoapods.org/making/using-pod-lib-create.html ( hold cmd and click links to open in a browser. ) What platform do you want to use?? [ iOS / macOS ]

ios What language do you want to use?? [ Swift / ObjC ]

swift Would you like to include a demo application with your library? [ Yes / No ]

yes Which testing frameworks will you use? [ Quick / None ] None

Would you like to do view based testing? [ Yes / No ]

No ``` 執行完上面命令後XCode會自動開啟專案,建立完成後不用管,關閉專案,將專案複製到庫資料夾裡,注:如果設定有顯示隱藏資料夾的,勿複製.git資料夾

三、配置.podspec檔案

進入剛剛複製的資料夾內開啟Example專案,結構如下

image.png

重要配置如下

s.homepage = 遠端倉庫地址 s.source = 遠端倉庫git地址 s.swift_version = '4.0' 其餘配置根據實際需求進行配置修改

四、驗證

開啟終端cd到倉庫目錄(Example上層目錄)

``` pod lib lint --allow-warnings

-> XCTest (1.0.0) - WARN | summary: The summary is not meaningful. - WARN | url: There was a problem validating the URL http://gitee.com/xxxx/xxxx. - WARN | [iOS] swift: The validator used Swift 4.0 by default because no Swift version was specified. To specify a Swift version during validation, add the swift_versions attribute in your podspec. Note that usage of a .swift-version file is now deprecated. - NOTE | xcodebuild: note: Using new build system - NOTE | xcodebuild: note: Building targets in parallel - NOTE | xcodebuild: note: Using codesigning identity override: - - 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')

XCTest passed validation. ``` 驗證成功

五、上傳程式碼到倉庫

1、這裡不多描述,重點打tag,重點打tag,重點打tag,重要打事情說三片。tag與配置檔案的版本一致

2、遠端驗證

``` pod spec lint --allow-warnings

-> XCTest (1.0.0) - WARN | summary: The summary is not meaningful. - NOTE | url: The URL (http://gitee.com/xxxx/xxxx) is not reachable. - WARN | [iOS] swift: The validator used Swift 4.0 by default because no Swift version was specified. To specify a Swift version during validation, add the swift_versions attribute in your podspec. Note that usage of a .swift-version file is now deprecated. - NOTE | xcodebuild: note: Using new build system - NOTE | xcodebuild: note: Building targets in parallel - NOTE | xcodebuild: note: Using codesigning identity override: - - 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')

Analyzed 1 podspec.

XCTest.podspec passed validation. ```

六、建立分支

1、由於我建立倉庫有區別,所以在拉下庫的時候沒有任何分支,所以這裡創面main分支后里面只保留README檔案,其餘全部刪除。提交到程式碼庫,請勿合併到master、提交到程式碼庫,請勿合併到master、提交到程式碼庫,請勿合併到master,重要到事情說三片。然後就不在管main分支。

2、進入碼雲倉庫管理頁面設定預設分支為main分支,預設分支很重要,預設分支很重要,預設分支很重要說三片

七、將遠端端私有索引庫(Repo)新增到本地的

pod repo add XCTest http://gitee.com/xxxx/xxxx.git 執行完成後,進入檔案~/.cocoapods/repos,檢視檔案結構如下

image.png

八、提交到遠端端私有索引庫

``` pod repo push XCTest XCTest.podspec --allow-warnings

Validating spec -> XCTest (1.0.0) - WARN | summary: The summary is not meaningful. - NOTE | url: The URL (http://gitee.com/xxxx/xxxx) is not reachable. - WARN | [iOS] swift: The validator used Swift 4.0 by default because no Swift version was specified. To specify a Swift version during validation, add the swift_versions attribute in your podspec. Note that usage of a .swift-version file is now deprecated. - NOTE | xcodebuild: note: Using new build system - NOTE | xcodebuild: note: Building targets in parallel - NOTE | xcodebuild: note: Using codesigning identity override: - - 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')

Updating the `XCTest' repo

Adding the spec to the `XCTest' repo

  • [Add] XCTest (1.0.0)

Pushing the `XCTest' repo ```

九、最後見證奇蹟一刻

再次搜尋庫名

pod search XCTest -> XCTest (1.0.0) A short description of XCTest. pod 'XCTest', '~> 1.0.0' - Homepage: http://gitee.com/xxxx/xxx - Source: http://gitee.com/xxxx/xxx.git - Versions: 1.0.0 [XCTest repo] 好了你要的私有庫已經建立完成,任意一個專案在Podfile配置如下:

``` source 'http://gitee.com/xxxx/xxx.git' platform :ios, ’9.0’

target 'Test' do pod 'XCTest'

end ``` 進入到專案裡面執行命令

pod install 或者 pod install --repo-update 即可在專案裡面使用你私有庫了。下一篇將學習檔案、資原始檔的使用和版本更新