創建Cocoapods私有庫


本文以自己在公司做的一個手勢密碼私有庫GesturePasswordKit為例說明。

 

1、在gitlab(或者github,我這里使用的例子是在gitlab上)上創建git倉庫 (確保授權正確,避免后續出現權限問題) 

 

2、使用sourcetree 把空倉庫clone到本地

 

3、將准備好的代碼拷貝進去(只留下需要的文件,建議使用一級目錄存儲), 需要外部調用的方法,要注意使用權限控制符號public /open

 

4、創建.podspec 文件 

       4.1 復制已有的 podspec 文件然后修改對應的參數

       4.2 執行命令行創建  pod spec create 【私有庫名稱】

       書寫正確的參數配置,如下圖:

Pod::Spec.new do |s|

  s.name         = "GesturePasswordKit"
  s.version      = "0.0.8"
  s.summary      = "手勢密碼"
  s.homepage     = "http://888888/GesturePasswordKit"
  s.license      = { :type => "MIT", :file => "LICENSE" } 
  s.description  = <<-DESC 
                        GesturePasswordKit 是一個用於實現手勢密碼管理的工具
                   DESC
  s.author       = { "wangzhitao" => "wangzhitao@888888.com" }
  s.platform     = :ios, "8.0"
  s.ios.deployment_target = "8.0"
  s.source       = { :git => "git@git.888888.com:iOS/GesturePasswordKit.git", :tag => 'v'+s.version.to_s}
  s.source_files = 'GesturePasswordKit/*.{png,h,m,swift}'     // 多級目錄使用:s.source_files = 'GesturePasswordKit/**/*'
  s.resources = "GesturePasswordKit/Resource.bundle"
  s.requires_arc = true
  s.dependency "Kingfisher"
  s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.0' }       // 指定swift版本,一般不需要寫

end

 

 

5、驗證podspec

      pod lib lint --allow-warnings 

      此時驗證時,gitLab上需要有podspec中的版本tag 

 

6、把代碼提交到git服務器

      git init

      git add .

      git commit -m "first commit"

      git push origin master

    可隨時使用git status 檢查文件狀態

 

7、打tag (命令行)

      git tag v0.0.1

      git push --tags

      git push origin master

      

     刪除tag

     git tag -d v0.0.1

     git push origin :refs/tags/v0.0.1

     git push origin master

 

8、添加你的 Podspec 到你的 repo

      pod repo add  GesturePasswordKitSpecs git@git.tuandai888.com:iOS/GesturePasswordKit.git

    

      在此之前先打tag

      pod repo push GesturePasswordKitSpecs GesturePasswordKit.podspec --allow-warnings

 

     // 檢查是否成功

     cd ~/.cocoapods

     open .

 

9、pod search GesturePasswordKit

 

#私有庫版本升級遇到的問題 swift3.0->4.0

1. 更新時遇到此錯誤,處理方法。 

[!] The spec did not pass validation, due to 1 error.

[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run: 

    `echo "2.3" > .swift-version`.

遇到上面的這種情況,直接使用下面的這條命令就可以解決問題了。

echo "4.0" > .swift-version

加入這句話后多了.swift-version文件

2.主工程podfile文件最后添加以下代碼,這段代碼的作用是,不同的庫,指定不同的版本,如有些第三方庫用3.2版本,有些升級為4.0版本,可以自定在 

swift_32  swift4
中添加,數組可為空,如swift_3.2 = ['RxSwift', 'RxCocoa']  swift4 = [] 說明除了 ['RxSwift', 'RxCocoa']其余庫都升級為4.0版本

添加下面這段代碼的原因是,上面 echo "4.0" > .swift-version 執行后,導入私有庫后pod install 會讓主工程的pod 一並升級到4.0,導致所有第三方都變成了4.0,編譯失敗。
swift_32 = ['RxSwift', 'RxCocoa']

swift4 = ['PalaverKit']

post_install do |installer|

  installer.pods_project.targets.each do |target|

    swift_version = nil

 

    if swift_32.include?(target.name)

      swift_version = '3.2'

    end

 

    if swift4.include?(target.name)

      swift_version = '4.0'

    end

 

    if swift_version

      target.build_configurations.each do |config|

        config.build_settings['SWIFT_VERSION'] = swift_version

      end

    end

  end

end

 

  公有庫的創建

  前面的步驟和私有pod一樣,在代碼commit並push之后:

  1、注冊trunk 

$ pod trunk register 郵箱 '用戶名' --description=‘性能監測庫’ 

    (1)郵箱為github上的登錄郵箱、用戶名為github上的用戶名。 

    (2)接收發送到郵箱的鏈接,點擊進入后注冊成功。

    (3)查看注冊的個人信息 : ‘$ pod trunk me ’

           注意podspec里面的用戶名和郵箱一定要和這里的對應,否則會后續驗證會報錯!

  2、驗證上傳到github上的公有倉庫是否有效。

$ pod spec lint cocoapodsName.podspec   --allow-warnings

  3、將公有倉庫推送到CocoaPods

$ pod trunk push cocoapodsName.podspec  --allow-warnings


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM