一、簡介
iOS的開發(fā)環(huán)境主要包括開發(fā)工具和包依賴管理工具的安裝。大部分的iOS開發(fā)人員選擇的開發(fā)工具都是Xcode。iOS的包依賴管理工具有Carthage和CocoaPods,他們的優(yōu)缺點在這里不再敖述,推薦使用CocoaPods。
二、Xcode
你可以在App Store中下載最新版本,也可以在Apple Developer中下載指定版本。
三、CocoaPods
CocoaPods的安裝會涉及到RVM、Ruby、RubyGems、Homebrew幾個概念。
RVM是一個命令行工具,可以提供一個便捷的多版本Ruby環(huán)境的管理和切換。
Ruby是一種開源的面向?qū)ο蟪绦蛟O(shè)計的服務(wù)器端腳本語言。
RubyGems是Ruby的一個包管理器,它提供一個分發(fā)Ruby程序和庫的標準格式,還提供一個管理程序包安裝的工具。
Homebrew是macOS(或 Linux)缺失的軟件包的管理器。
CocoaPods是Ruby語言寫的,所以安裝CocoaPods需要依賴Ruby環(huán)境,Mac電腦自帶Ruby環(huán)境不過版本可能偏低,你可以使用RVM或者Homebrew來管理Ruby,安裝RVM需要先安裝GPG,CocoaPods可以用HomeBrew或者RubyGems安裝。我們通常先安裝Homebrew,再通過Homebrew來安裝Ruby和CocoaPods。下面只簡單的羅列一些常用的命令,任何不清楚的地方都可以查閱詳細的資料。
查看Homebrew版本
$ brew --version
安裝Homebrew
$ /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
卸載Homebrew
$ /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"
查看Homebrew的源
# 查看brew.git源
$ git -C "$(brew --repo)" config --get remote.origin.url
# 查看homebrew-core.git源
$ git -C "$(brew --repo homebrew/core)" config --get remote.origin.url
# 查看homebrew-cask.git源
$ git -C "$(brew --repo homebrew/cask)" config --get remote.origin.url
Homebrew的官方源
# brew.git源
https://github.com/Homebrew/brew.git
# homebrew-core.git源
https://github.com/Homebrew/homebrew-core.git
# homebrew-cask.git源
https://github.com/Homebrew/homebrew-cask.git
替換Homebrew的源為中科大的源
# brew.git源
$ git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# homebrew-core.git源
$ git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# homebrew-cask.git源
$ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# 配置homebrew-bottles
## bash用戶
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
## zsh用戶
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
$ source ~/.zshrc
替換Homebrew的源為清華的源
# 替換brew.git源
$ git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# 替換 homebrew-core.git源
$ git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
# 替換 homebrew-cask.git源
$ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
# 使用bash的話
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
$ source ~/.bash_profile
# 使用zsh的話
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
更新Homebrew
$ brew update
查看Homebrew命令
$ brew --help
Homebrew查看已安裝軟件的列表
$ brew list
Homebrew搜索軟件
$ brew search xxx
Homebrew安裝軟件
$ brew install xxx
Homebrew卸載軟件:
$ brew uninstall xxx
Homebrew更新軟件
$ brew upgrade xxx
Homebrew查看可清理的舊版本包
$ brew cleanup -n
Homebrew清理所有包的舊版本
$ brew cleanup
Homebrew清理指定包的舊版本
$ brew cleanup $FORMULA
- Ruby(提醒:在進行Ruby的安裝或者更新之前,最好先更改gem的國內(nèi)源,否則卡的一_!?。。?/li>
查看Ruby版本
$ ruby --version
安裝Ruby
$ brew install ruby
卸載Ruby
$ brew uninstall ruby
更新Ruby
$ brew upgrade ruby
查看Ruby命令
$ ruby --help
查看RubyGems的源
$ gem sources -l
移除RubyGems的源
$ gem sources --remove xxx
添加RubyGems的國內(nèi)源
$ gem sources -a https://gems.ruby-china.com
查看RubyGems版本
$ gem --version
更新RubyGems
$ gem update
RubyGems查看已安裝軟件的列表
$ gem list
RubyGems搜索軟件
$ gem search xxx
RubyGems安裝軟件
$ sudo gem install xxx
RubyGems卸載軟件:
$ sudo gem uninstall xxx
查看CocoaPods版本
$ pod --version
使用Homebrew安裝/卸載/更新CocoaPods
安裝CocoaPods
$ brew install cocoapods
卸載CocoaPods
$ brew uninstall cocoapods
更新CocoaPods
$ brew upgrade cocoapods
使用RubyGems安裝/卸載/更新CocoaPods
安裝CocoaPods
$ sudo gem install cocoapods
卸載CocoaPods
$ gem uninstall cocoapods
更新CocoaPods
$ gem update
$ sudo gem install cocoapods
查看CocoaPods命令
$ pod --help
建立CocoaPods的本地索引庫
$ pod setup
查看CocoaPods的本地索引庫
$ pod repo list
更新CocoaPods的本地索引庫
$ pod repo update
更新CocoaPods的特定repo的本地索引庫
$ pod repo update xxx
經(jīng)常使用的pod命令
pod init:創(chuàng)建podfile文件
pod install:三方庫的版本會受到podfile和podfile.lock兩個文件的約束,執(zhí)行pod install命令會在滿足兩個文件的版本約束情況下查詢本地索引庫中最新的版本,同時將版本信息記錄在podfile.lock文件中,如果不存在podfile.lock文件會自動創(chuàng)建podfile.lock文件。
pod install --no-repo-update:會在pod install命令的基礎(chǔ)上跳過版本的更新,如果本地存在滿足版本約束的低版本會直接使用。
pod update:忽略podfile.lock文件,直接從遠程索引庫中去找符合podfile文件約束的最新版本,并更新本地索引庫的對應(yīng)信息。
pod版本指定的約束
pod 'XXX':不指定版本,表示希望使用最新版本
pod 'XXX', '1.0.0':指定明確版本,表示只想要這個版本
邏輯關(guān)系
pod 'XXX', '> 1.0.0': 版本號大于1.0.0的
pod 'XXX', '>= 1.0.0':版本號大于等于1.0.0的
pod 'XXX', '< 1.0.0':版本號小于1.0.0的
pod 'XXX', '<= 1.0.0':版本號小于等于1.0.0的
最優(yōu)匹配
pod 'XXX', '~> 1.0.0':版本號大于等于1.0.0并且小于1.1.0的
pod 'XXX', '~> 1.0':版本號大于等于1.0并且小于2.0的
pod 'XXX', '~> 1:版本號大于等于1的
其他信息
CocoaPods的本地索引庫所在位置:/Users/用戶/.cocoapods
CocoaPods的本地倉庫所在位置:/Users/用戶/Library/Caches/CocoaPods