# CocoaPods 建立私有 Pods 管理 > [time=Mon, Mar 12, 2018 12:22 PM] ###### tags: `Xcode` `CocoaPods` CocoaPods 預設使用 `GitHub` 作為公開 Pods 管理庫,但因為需要付費才能使用 `Private Project`,所以選用 [GitLab](https://gitlab.com) 來製作 Private Project。 以下範例會以 `log` 作為範例名稱。 ### 1. 建立兩個 Private Project 1. `iOSPods` 2. `log` ![](https://i.imgur.com/boFoFS1.png) ### 2. 寫好 Code 並上傳至 Git ![](https://i.imgur.com/atX7i2v.png) ### 3. 將專案上 tag 因為 `CocoaPods` 是依賴 `tag` 分辨版本,在日後的版本更新只需要再上一個 `tag` 即可。 ``` git tag "1.0.0" git push --tags ``` ### 4. 註冊 CocoaPods `trunk` 需要 `CocoaPods 0.33` 版本以上,用 `pod --version` 檢查版本,如果版本過低,需要升级。 ``` pod --version //如果版本低於 0.33 sudo gen install cocoapods pod setup ``` 檢查自己是否註冊 ``` pod trunk me ``` 如果沒有這註冊可使用指令 ``` // 加上 --verbose 可以輸出詳細錯誤訊息 pod trunk register {email} '{name}' --verbose ``` 註冊後需要到信箱點選驗證信,可使用 `pod trunk me` 檢查是否完成註冊。 ### 5. 建立 .podspec `cd` 到專案的目錄 ``` pod spec create log ``` ### 6. 編輯 .podspec 使用記事本或 `vim` 編輯 `.podspec` 修改或用以下範例 ``` Pod::Spec.new do |s| s.name = 'Log' s.version = '1.0.0' s.authors = { 'CK' => 'a@a.com' } s.homepage = 'https://gitlab.com/YourName/log' s.summary = 'log' s.source = { :git => 'https://gitlab.com/YourName/log.git', :tag => s.version.to_s } s.license = { :type => "MIT", :file => "LICENSE" } s.platform = :ios, '9.0' s.requires_arc = true s.source_files = 'Log' s.public_header_files = 'Log/*.h' s.ios.deployment_target = '9.0' end ``` * `s.name`:名稱,`pod search` 搜尋的關鍵字,這裡一定要跟 `.podspec` 的名稱一樣。 * `s.version`:版本,to_s:返回一個字串。 * `s.author`:作者。 * `s.homepage`:Git 主頁 * `s.summary`:簡介 * `s.source`:程式的 Git * `s.license`:使用許可證 * `s.platform`:支援平台 * `s.requires_arc`:是否支援 ARC * `s.source_files`:需要包含的源文件 * `s.public_header_files`:需要包含的文件 * `s.ios.deployment_target`:支援的 pod 最低版本 ``` s.license= { :type => "MIT", :file => "LICENSE" } 建議這樣寫,如果寫別的會出現警告,導致失敗。 ``` source_files 寫法及含義 ``` "log/*" "log/log/*.{h,m}" "log/**/*.h" ``` `*` 表示匹配所有文件 `*.{h,m}` 表示匹配所有以 `.h` 和 `.m` 結尾的文件 `**` 表示匹配所有子目錄 s.source 常見寫法 ``` s.source = { :git => "https://gitlab.com/YourName/log.git", :commit => "9cf66bd6" } s.source = { :git => "https://gitlab.com/YourName/log.git", :tag => 1.0.0 } s.source = { :git => "https://gitlab.com/YourName/log.git", :tag => s.version } ``` * `commit => "9cf66bd6"` 表示過這個 `Pod` 版本與 `Git` 中某個 `commit` 綁定 * `tag => 1.0.0` 表示將這個 `Pod` 版本與 `Git` 中某個版本的 `commit` 綁定 * `tag => s.version` 表示將這個 `Pod` 版本與 `Git` 中某個相同版本的 `commit` 綁定 ### 7. 驗證 .podspec 到此檢查一下專案內有以下的文件 1. 專案內容 2. `.podspec` 3. `LICENSE` 測試 `.podspec` 文件是否存在語法錯誤。 ``` pod spec lint log.podspec --verbose ``` ### 8. 新增私有 pod 位置 `{git}` 位置為 `iOSPods` 的位置 ``` pod repo add {name} {git} ``` 列出已有的 `repo` 清單 ``` pod repo list ``` ### 9. 發布 pod 到私有環境 `{name}` 同步驟 `8.` 的 `{name}` ``` pod repo push {name} ``` ### 10. 測試自己的 Cocoapods * 建立一個測試專案,`cd` 到專案的目錄 `pod init` 建立 `Podfile` 文件。 * 新增 `source '{https://gitlab.com/YourName/iOSPods}'`,引用 `pod 'log'` * `pod install` --- * 參考 https://www.jianshu.com/p/89605e02bf18 * 參考 https://www.jianshu.com/p/3a365f273439 * 參考 https://segmentfault.com/a/1190000007947371