CocoaPods

本文對應github地址CocoaPods,如果由于github調整導致資源找不到,請訪問github

一、源設置

  1. 查看源,終端輸入: sudo gem sources -l
  2. 刪除已有的源,如: sudo gem sources --remove https://rubygems.org/
  3. 添加國內鏡像,如: sudo gem sources --add https://gems.ruby-china.com
  4. 看是否真的添加上: sudo gem sources -l
  5. 若出現 https://gems.ruby-china.com 則添加成功

二、升級(按需選中執行)

  • 升級homebrew
    1. 查看版本 brew --version
    2. 卸載brew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
    3. 安裝brew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    4. 查看版本 brew --version
    5. 安裝目錄 which brew
  • 升級rvm
    1. 查看版本 rvm --version
    2. 升級RVM curl -L https://get.rvm.io | bash -s stable
    3. 生效RVM source ~/.rvm/scripts/rvm
    4. 重載RVM rvm reload
    5. 查看版本 rvm --version
    6. 安裝目錄 which rvm
  • 升級ruby
    1. 查看版本 ruby --version
    2. 已知版本 rvm list known
    3. 安裝指定 rvm install 2.6.3 或者 rvm install ruby-2.6.3
    4. 已安列別 rvm list
    5. 設定默認 rvm use 2.6.3 --default
    6. 刪除多余 rvm remove 2.0.0
    7. 安裝目錄 which ruby
  • 手動安裝ruby
    1. 下載ruby ruby下載地址
    2. 進入解壓文件夾執行 ./configure
    3. 編譯 sudo make
    4. 安裝 sudo make install
  • 升級openssl
    1. 查看版本 openssl version
    2. 升級版本 brew upgrade openssl
    3. 鏈接新版 brew link openssl --force
    4. 查看版本 openssl version
    5. 安裝目錄 which openssl
  • 升級gem
    1. 查看版本 gem --version
    2. 升級版本 gem update --system
    3. 查看版本 gem --version
  • 升級cocoaPods
    1. 查看版本 pod --version
    2. 升級版本 sudo gem update cocoapods
    3. 查看版本 pod --version
    4. 安裝目錄 which pod

三、安裝

  • 安裝xcodeproj sudo gem install xcodeproj
  • 安裝cocoapods sudo gem install cocoapods
  • 安裝pre版 sudo gem install cocoapods --pre
  • 執行setup pod setup

四、錯誤解決

  • 權限問題

    • 添加 sudo

    • 關閉SIP

      1. 重啟系統,按住 Command + R 進入恢復模式;
      2. 點擊頂部菜單欄 實用工具 中的 終端;
      3. 輸入 csrutil disable 來禁用 SIP 保護機制;
      4. 重啟系統
      5. 如果想重新開啟按上述步驟,然后執行 csrutil enable
  • undefined method 'size' for nil :NilClass(No MethodError)

    • 刪除緩存

      1. 查看gem環境: gem env

        顯示中找到Gem Paths

        - GEM PATHS:
            - /Library/Ruby/Gems/2.3.0
            - /Users/zsl/.gem/ruby/2.3.0
        
      2. 刪除對應目錄下cache

        進入第一個目錄 cd /usr/local/ruby/lib/ruby/gems/2.1.0
        刪除第一個目錄cache rm -rf cache
        進入第二個目錄 cd /home/vagrant/.gem/ruby/2.1.0
        刪除第二個目錄cache rm -rf cache

      3. 回到用戶目錄: cd ~

  • error: RPC failed; curl56SSLRead()returnerror -36

    • 終端輸入 sudo xcode-select --print-path 查看Xcode路徑
    • 復制路徑,執行 sudo xcode-select -switch 路徑
      如: sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
    • 執行 sudo rm -fr ~/.cocoapods/repos/master 刪除repos目錄下master
    • 再執行 pod setup
    • 如果路徑是 /Library/Developer/CommandLineTools可切換到Xcode.app路徑
  • Operation not permitted - /usr/bin/xcodeproj

    自定義GEM_HOME

    命令行操作:

    • 建立目錄 mkdir -p $HOME/Software/ruby
    • 添加路徑 export GEM_HOME=$HOME/Software/ruby
    • 執行安裝xcodeproj gem install xcodeproj (出現 1 gem installed)
    • 指定目錄安裝cocoapods sudo gem install -n /usr/local/bin cocoapods
    • 添加路徑 export PATH=$PATH:$HOME/Sofware/ruby/bin
    • 查看版本 pod --version
  • RPC failed; curl 56 SSLRead() return error -9806

    終端依次執行下面命令(不包括美元符號)

    $ brew remove git
    $ brew remove curl
    $ brew install openssl
    $ brew install --with-openssl curl
    $ brew install --with-brewed-curl --with-brewed-openssl git
    
  • 搜索不到存在的某個庫(特別是剛提交的)

    執行 rm /Users/zsl/Library/Caches/CocoaPods/search_index.json zsl改成自己的

四、使用cocoaPods

  • 進入工程目錄 cd ~/DeskTop/TestProject

  • 建立Podfile: touch Podfile (必須Podfile)

  • 搜索三方庫:pod search AFNetworking (復制內容 ‘AFNetworking’, ‘~>2.5.3’)

  • 打開Podfile: open -e Podfile 并粘貼復制的內容

  • Podfile格式如下

    # 私有
    source 'http://git.idengyun.com/iOSTeam/KNSpec.git'
    # 三方    
    source 'https://github.com/CocoaPods/Specs.git'
    
    platform :ios, '8.0'
    
    inhibit_all_warnings!
    
    # 如果@import Charts; 方式導入可以不用use_frameworks!生成庫文件
    # use_frameworks!
    
    # 每個工程使用自己的cocopods版本
    # gem 'cocoapods', '1.7.5'
    
    xcodeproj 'TestProject.xcodeproj'
    
    target 'Target1' do
        
        pod 'DDYQRCode', '~> 1.1.1'
    end
    
    target 'Target2' do
        
        pod 'DDYCamera', '~> 1.1.1'
    end
    
    =begin
    這里寫多行注釋
    =end
    
    
  • 執行 pod install --verbose --no-repo-update

  • 需要更新才用此步驟,更新 pod update --verbose --no-repo-update

五、讓自己的庫支持cocoapods管理

  • 進入工程目錄

  • 建立 .podspec 文件 pod spec create DDYQRCode

  • 填寫內容

    Pod::Spec.new do |ddyspec|
        ddyspec.name         = 'DDYQRCode'
        ddyspec.version      = '1.1.1'
        ddyspec.summary      = '二維碼/條形碼生成'
        ddyspec.homepage     = 'https://github.com/RainOpen/DDYQRCode'
        ddyspec.license      = 'MIT'
        ddyspec.authors      = {'Rain' => '634778311@qq.com'}
        ddyspec.platform     = :ios, '8.0'
        ddyspec.source       = {:git => 'https://github.com/RainOpen/DDYQRCode.git', :tag => ddyspec.version}
        ddyspec.source_files = 'DDYQRCode/DDYQRCode/*.{h,m}'
        ddyspec.resource     = 'DDYQRCode/DDYQRCode/DDYQRCode.bundle'
        ddyspec.requires_arc = false
        # s.framework  = "AVFoundation"
        # s.frameworks = "UIKit", "AVFoundation"
        # s.dependency "JSONKit", "~> 1.4"
    end
    
  • 校驗格式是否正確 pod spec lint DDYQRCode.podspec --verbose

  • 注冊Trunk pod trunk register 634778311@qq.com 'Rain' --description='iOS' --verbose

  • 郵箱點擊鏈接驗證

  • Trunk信息 pod trunk me

  • 多人維護添加成員 pod trunk add-owner ARAnalytics kyle@cocoapods.org

  • 發布到pod Trunck pod trunk push DDYQRCode.podspec

  • 如果必要的話更新pod pod setup

  • 刪除單個版本 pod trunk delete PODNAME VERSION

  • 刪除所有版本 pod trunk deprecate PODNAME (有提示回復y)

六、卸載指定版本

  • 查看cocoapods相關安裝 gem list --local | grep cocoapods
  • 刪除相關 sudo gem uninstall cocoapods-deintegrate
  • 移除指定 sudo gem uninstall cocoapods -v 1.1.0

setup加速

  1. gitee鏡像
    pod repo remove master   
    pod repo add master https://gitee.com/mirrors/CocoaPods-Specs   
    pod repo update   
    
  2. 清華鏡像
```
pod repo remove master   
pod repo add master https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git   
pod repo update   
```
  1. iOS工程如果有Podfile要更改
    source 'https://gitee.com/mirrors/CocoaPods-Specs   
    .git'
    source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'
    

參考

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容