以 CocoaPods 建立 Private Pods

Create Private Pods with CocoaPods

  • Create a repository to save our podspec file
    建立一個遠端儲藏庫。可以是GitHubbitbucket或你自己的 git server。

  • Install CocoaPods
    如果你還沒有安裝 CocoaPods 工具,打開終端機(terminal)輸入以下指令安裝之。

    1
    sudo gem install cocoapods
  • Create a private pod repository with CocoaPods

    1
    pod repo add REPO_NAME SOURCE_URL

    這個步驟是指定 framework 檔的私人上傳位址。當然,如果你想把 framework 公開,就把它上傳至官方的 Spec Repo

  • Check your private repository

    1
    pod repo list

    用以上指令確認自己的 private pods repository 是否存在。

Create a podspec file

  • Init a podspec file in terminal
    建立一個 podspec 檔。

    1
    pod spec create NAME
  • Edit podspec file
    參考官網編輯 podspec,記得要修改 summary,description 要比 summary 長。
    另外,如果你的 framework 裡面需要使用其他的第三方套件,記得增加 dependency 項目。

  • Verify podspec file

    1
    pod lib lint

    以上述指令確認 podspec 檔是否合乎規定。如果合乎規定, terminal 會出現以下訊息:validation passed

  • Push the podspec file to Cocoapods private repository
    確認 podspec 合乎規定後,就可以上傳啦。

    1
    pod repo push [REPO_NAME] [NAME.podspec]

    其中 REPO_NAME 是你的私人儲藏褲名稱。如果你要公開 framework 的話,就是用下面的指令:(需先註冊

    1
    pod trunk push [NAME.podspec]

    不過公開的部分不在這次討論範圍,所以不多說 顯示為不負責任 XD

Test your framework

  • Create a single view application Xcode project

  • Init a Podfile
    在上一步的 project 路徑下,輸入以下指令:

    1
    pod init
  • Modify Podfile
    編輯 Podfile,在 target ‘projectName’ do 下方加入以下指令:

    1
    pod ‘frameworkName’
  • Install Podfile

    1
    pod install
  • Verify framework by Writing test code in the test project
    最後就是自己寫一些測試程式碼,試試看自己的 framework 功能囉。