[环境] Mac 中 iOS 环境搭建

2022-05-31  本文已影响0人  巨馍蘸酱

iOS 开发环境搭建 (仅限macOS)

Xcode

在 App Store 下载开发工具 Xcode

Homebrew (brew)

Homebrew 能在Mac中方便的安装软件或者卸载软件. 相当于Linux的yum、apt-get等软件管理工具.
Homebrew 将这些工具统统安装到了 /usr/local/Cellar 目录中, 并在 /usr/local/bin 中创建符号链接.

使用官方安装 Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

 ✘ cityfruit@shiyanchaodeMBP  ~  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password)...
Password:
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew

Press RETURN/ENTER to continue or any other key to abort:
==> /usr/bin/sudo /usr/sbin/chown -R cityfruit:admin /usr/local/Homebrew
==> Downloading and installing Homebrew...
remote: Enumerating objects: 64, done.
remote: Counting objects: 100% (56/56), done.
remote: Total 64 (delta 56), reused 56 (delta 56), pack-reused 8
Unpacking objects: 100% (64/64), 37.48 KiB | 155.00 KiB/s, done.
From https://github.com/Homebrew/brew
   e41dade42..405472803  master     -> origin/master
HEAD is now at 405472803 Merge pull request #13355 from Bo98/fake-el-capitan
Updated 1 tap (homebrew/core).
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

 cityfruit@shiyanchaodeMBP  ~  brew --version
Homebrew 3.4.11
Homebrew/homebrew-core (git revision 370503e202f; last commit 2022-06-01)
 cityfruit@shiyanchaodeMBP  ~  which brew
/usr/local/bin/brew
 cityfruit@shiyanchaodeMBP  ~  

Homebrew 国内安装脚本

# 查看当前 brew.git 源
➜  ~ cd "$(brew --repo)" && git remote -v
origin  https://github.com/Homebrew/brew.git (fetch)
origin  https://github.com/Homebrew/brew.git (push)


# 使用中科大源 brew.git
➜  ~ cd "$(brew --repo)" && git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 修改 homebrew-core.git
➜  ~ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" && git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 替换 brew bintray 镜像
➜  ~ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile && source ~/.bash_profile

# 立刻生效
➜  ~ brew update

=================

# 查看(诊断) brew 状态
➜  ~ brew doctor

# reset
➜  ~ cd "$(brew --repo)" && git fetch && git reset --hard origin/master

# 重置 brew.git 为官方源
➜  ~ git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

# 重置 homebrew-core.git 为官方源
➜  ~ git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git

# 重置 homebrew-cask.git 为官方源
➜  ~ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask

# bash 注释掉 HOMEBREW_BOTTLE_DOMAIN 配置
➜  ~ vi ~/.bash_profile
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx

# 刷新源
➜  ~ brew update

Homebrew 常用命令

Ruby Version Manager (rvm)

Ruby Version Manager 是一个命令行工具, 允许您轻松地安装管理和使用多个 ruby 环境.
虽然 macOS 自带了一个ruby环境, 但是那是系统自己使用的, 所以权限很小, 只有 system. 而/Library目录是root权限, 所以很多会提示无权限.

curl -L get.rvm.io | bash -s stable #安装 rvm
source ~/.bashrc
source ~/.bash_profile
source ~/.rvm/scripts/rvm
rvm -v
rvm get stable # 升级 rvm
rvm list known # 列出ruby可安装的版本信息
rvm install 2.7.0
rvm uninstall 2.7.0
rvm use 2.7.0 --default
rvm implode # 卸载 rvm

 ✘ cityfruit@shiyanchaodeMBP  ~  curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    196      0 --:--:-- --:--:-- --:--:--   197
100 24535  100 24535    0     0    122      0  0:03:21  0:03:20  0:00:01  4772
Downloading https://github.com/rvm/rvm/archive/1.29.12.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.12/1.29.12.tar.gz.asc
curl: (28) Connection timeout after 30005 ms
curl: (28) Connection timeout after 30005 ms
Found PGP signature at: 'https://github.com/rvm/rvm/releases/download/1.29.12/1.29.12.tar.gz.asc',
but no GPG software exists to validate it, skipping.
Installing RVM to /Users/cityfruit/.rvm/
    Adding rvm PATH line to /Users/cityfruit/.profile /Users/cityfruit/.mkshrc /Users/cityfruit/.bashrc /Users/cityfruit/.zshrc.
    Adding rvm loading line to /Users/cityfruit/.profile /Users/cityfruit/.bash_profile /Users/cityfruit/.zlogin.
Installation of RVM in /Users/cityfruit/.rvm/ is almost complete:

  * To start using RVM you need to run `source /Users/cityfruit/.rvm/scripts/rvm`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
Thanks for installing RVM 🙏
Please consider donating to our open collective to help us maintain RVM.

👉  Donate: https://opencollective.com/rvm/donate


 cityfruit@shiyanchaodeMBP  ~  
 cityfruit@shiyanchaodeMBP  ~  source ~/.bash_profile 
 cityfruit@shiyanchaodeMBP  ~  source ~/.bashrc 
 cityfruit@shiyanchaodeMBP  ~  rvm --version
rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
 cityfruit@shiyanchaodeMBP  ~   cityfruit@shiyanchaodeMBP  ~  rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.10]
[ruby-]2.5[.8]
[ruby-]2.6[.6]
[ruby-]2.7[.2]
[ruby-]3[.0.0]
ruby-head
...

 cityfruit@shiyanchaodeMBP  ~  echo "export rvm_max_time_flag=20" >> ~/.rvmrc
 cityfruit@shiyanchaodeMBP  ~  rvm install 3.0                               
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/12.3/x86_64/ruby-3.0.0.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Certificates bundle '/usr/local/etc/openssl@1.1/cert.pem' is already up to date.
Requirements installation successful.
Installing Ruby from source to: /Users/cityfruit/.rvm/rubies/ruby-3.0.0, this may take a while depending on your cpu(s)...
ruby-3.0.0 - #downloading ruby-3.0.0, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:30 --:--:--     0
curl: (28) Connection timeout after 30001 ms
Warning: Problem : timeout. Will retry in 2 seconds. 3 retries left.
  0     0    0     0    0     0      0      0 --:--:--  0:00:30 --:--:--     0
curl: (28) Connection timeout after 30003 ms
Warning: Problem : timeout. Will retry in 2 seconds. 2 retries left.
100 18.6M  100 18.6M    0     0  2956k      0  0:00:06  0:00:06 --:--:-- 3920k
ruby-3.0.0 - #extracting ruby-3.0.0 to /Users/cityfruit/.rvm/src/ruby-3.0.0 - please wait
ruby-3.0.0 - #configuring - please wait
ruby-3.0.0 - #post-configuration - please wait
ruby-3.0.0 - #compiling - please wait
ruby-3.0.0 - #installing - please wait
ruby-3.0.0 - #making binaries executable - please wait
Installed rubygems 3.2.3 is newer than 3.0.9 provided with installed ruby, skipping installation, use --force to force installation.
ruby-3.0.0 - #gemset created /Users/cityfruit/.rvm/gems/ruby-3.0.0@global
ruby-3.0.0 - #importing gemset /Users/cityfruit/.rvm/gemsets/global.gems - please wait
ruby-3.0.0 - #generating global wrappers - please wait
ruby-3.0.0 - #gemset created /Users/cityfruit/.rvm/gems/ruby-3.0.0
ruby-3.0.0 - #importing gemsetfile /Users/cityfruit/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-3.0.0 - #generating default wrappers - please wait
ruby-3.0.0 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-3.0.0 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri
 cityfruit@shiyanchaodeMBP  ~  ruby -v
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin21]
 cityfruit@shiyanchaodeMBP  ~  which ruby
/Users/cityfruit/.rvm/rubies/ruby-3.0.0/bin/ruby
 cityfruit@shiyanchaodeMBP  ~  rvm use 3.0 --default
Using /Users/cityfruit/.rvm/gems/ruby-3.0.0
 cityfruit@shiyanchaodeMBP  ~  

RubyGems (gem)

brew用于操作系统层面上的软件包的安装, 而gem只是管理ruby软件

Ruby自带gem环境, 即安装了Ruby就已经安装了gem

RubyGems简称gem, 是一个管理Ruby库和程序的标准包, 它通过Ruby Gem(如 https://rubygems.org/ )源来查找、安装、升级和卸载软件包, 非常的便捷.

CocoaPods (pod)

CocoaPods 是用 Ruby 构建的, 是一个用来管理 iOS 第三方依赖库的工具. 项目源码在 GitHub https://github.com/CocoaPods

CocoaPods 的原理是将所有的依赖库都放到另一个名为 Pods 的项目中, 然后让主项目依赖 Pods 项目, 这样就把源码管理工作从主项目移到了 Pods 项目中.

第三方库会被编译成 .a 静态库或者 .framwork 的动态链接库供我们真正的工程使用.

CocoaPods 会将所有的第三方库以 target 的方式组成一个名为 Pods 的工程, 该工程就放在刚才新生成的 Pods 目录下. 整个第三方库工程会生成一个名称为 libPods.a 的静态库供我们的工程使用.

对于资源文件, CocoaPods 提供了一个名为 Pods-resources.sh 的 bash 脚本, 该脚本在项目每次编译的时候都会执行, 将第三方库的各种资源文件复制到目标目录中.

原来的工程设置已经被更改了, 这时候我们直接打开原来的工程文件去编译就会报错. 我们的工程和第三方库所在的 Pods 工程会被以一个新生成的 workspace 的形式组织和管理, 方便我们直观的管理工程和第三方库.

CocoaPods 通过一个名为 Pods.xcconfig 的文件来在编译时设置所有的依赖和参数.

安装

使用 Homebrew 安装

  1. 安装 Homebrew /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. 安装 CocoaPods brew install cocoapods
  3. 安装本地 pod 资源库 git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git ~/.cocoapods/repos/trunk

使用 Gem 安装

  1. 安装 rvm curl -L get.rvm.io | bash -s stable
  2. 查看 ruby 可用版本 rvm list known
  3. 安装 ruby rvm install 3.0.0
  4. 设置默认 ruby rvm use 3.0.0 --default
  5. 更新 sudo gem update --system
  6. 检查源 gem sources -l
  7. 安装 CocoaPods sudo gem install -n /usr/local/bin cocoapods
  8. 安装本地 pod 资源库 pod setup (已经失效),
    • 使用国内镜像 git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git ~/.cocoapods/repos/trunk
    • 使用官方镜像 git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/trunk
  9. 更新 sudo gem update cocoapods
  10. 查看 gem list --local | grep cocoapods
  11. 卸载 sudo gem uninstall cocoapods

直接使用系统内置 ruby 安装 pod 报错, 需要自己重新安装一下 ruby

 ✘ cityfruit@shiyanchaodeMBP  ~  sudo gem install -n /usr/local/bin cocoapods
Password:
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0 directory.
 ✘ cityfruit@shiyanchaodeMBP  ~   
 cityfruit@shiyanchaodeMBP  ~  ruby -v
ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.x86_64-darwin21]
 cityfruit@shiyanchaodeMBP  ~  which ruby
/usr/bin/ruby
 cityfruit@shiyanchaodeMBP  ~  

使用 rvm 安装了 ruby 3.0 后, 安装 pod

 cityfruit@shiyanchaodeMBP  ~  rvm install 3.0
 cityfruit@shiyanchaodeMBP  ~  ruby -v
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin21]
 cityfruit@shiyanchaodeMBP  ~  
 cityfruit@shiyanchaodeMBP  ~  which ruby
/Users/cityfruit/.rvm/rubies/ruby-3.0.0/bin/ruby
 cityfruit@shiyanchaodeMBP  ~  
 cityfruit@shiyanchaodeMBP  ~  sudo gem install -n /usr/local/bin cocoapods
Password:
Fetching nanaimo-0.3.0.gem
Fetching colored2-3.1.2.gem
Fetching claide-1.1.0.gem
Fetching CFPropertyList-3.0.5.gem
Fetching atomos-0.1.3.gem
Fetching ruby-macho-2.5.1.gem
Fetching xcodeproj-1.21.0.gem
Fetching nap-1.1.0.gem
Fetching molinillo-0.8.0.gem
Fetching gh_inspector-1.1.3.gem
Fetching fourflusher-2.3.1.gem
Fetching escape-0.0.4.gem
Fetching cocoapods-try-1.2.0.gem
Fetching netrc-0.11.0.gem
Fetching cocoapods-trunk-1.6.0.gem
Fetching cocoapods-search-1.0.1.gem
Fetching cocoapods-plugins-1.0.0.gem
Fetching cocoapods-downloader-1.6.3.gem
Fetching cocoapods-deintegrate-1.0.5.gem
Fetching ffi-1.15.5.gem
Fetching ethon-0.15.0.gem
Fetching typhoeus-1.4.0.gem
Fetching public_suffix-4.0.7.gem
Fetching fuzzy_match-2.0.4.gem
Fetching concurrent-ruby-1.1.10.gem
Fetching httpclient-2.8.3.gem
Fetching algoliasearch-1.27.5.gem
Fetching addressable-2.8.0.gem
Fetching zeitwerk-2.5.4.gem
Fetching tzinfo-2.0.4.gem
Fetching i18n-1.10.0.gem
Fetching activesupport-6.1.6.gem
Fetching cocoapods-1.11.3.gem
Fetching cocoapods-core-1.11.3.gem
Successfully installed nanaimo-0.3.0
Successfully installed colored2-3.1.2
Successfully installed claide-1.1.0
Successfully installed CFPropertyList-3.0.5
Successfully installed atomos-0.1.3
Successfully installed xcodeproj-1.21.0
Successfully installed ruby-macho-2.5.1
Successfully installed nap-1.1.0
Successfully installed molinillo-0.8.0
Successfully installed gh_inspector-1.1.3
Successfully installed fourflusher-2.3.1
Successfully installed escape-0.0.4
Successfully installed cocoapods-try-1.2.0
Successfully installed netrc-0.11.0
Successfully installed cocoapods-trunk-1.6.0
Successfully installed cocoapods-search-1.0.1
Successfully installed cocoapods-plugins-1.0.0
Successfully installed cocoapods-downloader-1.6.3
Successfully installed cocoapods-deintegrate-1.0.5
Building native extensions. This could take a while...
Successfully installed ffi-1.15.5
Successfully installed ethon-0.15.0
Successfully installed typhoeus-1.4.0
Successfully installed public_suffix-4.0.7
Successfully installed fuzzy_match-2.0.4
Successfully installed concurrent-ruby-1.1.10
Successfully installed httpclient-2.8.3
A new major version is available for Algolia! Please now use the https://rubygems.org/gems/algolia gem to get the latest features.
Successfully installed algoliasearch-1.27.5
Successfully installed addressable-2.8.0
Successfully installed zeitwerk-2.5.4
Successfully installed tzinfo-2.0.4
Successfully installed i18n-1.10.0
Successfully installed activesupport-6.1.6
Successfully installed cocoapods-core-1.11.3
Successfully installed cocoapods-1.11.3
Parsing documentation for nanaimo-0.3.0
Installing ri documentation for nanaimo-0.3.0
Parsing documentation for colored2-3.1.2
Installing ri documentation for colored2-3.1.2
Parsing documentation for claide-1.1.0
Installing ri documentation for claide-1.1.0
Parsing documentation for CFPropertyList-3.0.5
Installing ri documentation for CFPropertyList-3.0.5
Parsing documentation for atomos-0.1.3
Installing ri documentation for atomos-0.1.3
Parsing documentation for xcodeproj-1.21.0
Installing ri documentation for xcodeproj-1.21.0
Parsing documentation for ruby-macho-2.5.1
Installing ri documentation for ruby-macho-2.5.1
Parsing documentation for nap-1.1.0
Installing ri documentation for nap-1.1.0
Parsing documentation for molinillo-0.8.0
Installing ri documentation for molinillo-0.8.0
Parsing documentation for gh_inspector-1.1.3
Installing ri documentation for gh_inspector-1.1.3
Parsing documentation for fourflusher-2.3.1
Installing ri documentation for fourflusher-2.3.1
Parsing documentation for escape-0.0.4
Installing ri documentation for escape-0.0.4
Parsing documentation for cocoapods-try-1.2.0
Installing ri documentation for cocoapods-try-1.2.0
Parsing documentation for netrc-0.11.0
Installing ri documentation for netrc-0.11.0
Parsing documentation for cocoapods-trunk-1.6.0
Installing ri documentation for cocoapods-trunk-1.6.0
Parsing documentation for cocoapods-search-1.0.1
Installing ri documentation for cocoapods-search-1.0.1
Parsing documentation for cocoapods-plugins-1.0.0
Installing ri documentation for cocoapods-plugins-1.0.0
Parsing documentation for cocoapods-downloader-1.6.3
Installing ri documentation for cocoapods-downloader-1.6.3
Parsing documentation for cocoapods-deintegrate-1.0.5
Installing ri documentation for cocoapods-deintegrate-1.0.5
Parsing documentation for ffi-1.15.5
Installing ri documentation for ffi-1.15.5
Parsing documentation for ethon-0.15.0
Installing ri documentation for ethon-0.15.0
Parsing documentation for typhoeus-1.4.0
Installing ri documentation for typhoeus-1.4.0
Parsing documentation for public_suffix-4.0.7
Installing ri documentation for public_suffix-4.0.7
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for concurrent-ruby-1.1.10
Installing ri documentation for concurrent-ruby-1.1.10
Parsing documentation for httpclient-2.8.3
Installing ri documentation for httpclient-2.8.3
Parsing documentation for algoliasearch-1.27.5
Installing ri documentation for algoliasearch-1.27.5
Parsing documentation for addressable-2.8.0
Installing ri documentation for addressable-2.8.0
Parsing documentation for zeitwerk-2.5.4
Installing ri documentation for zeitwerk-2.5.4
Parsing documentation for tzinfo-2.0.4
Installing ri documentation for tzinfo-2.0.4
Parsing documentation for i18n-1.10.0
Installing ri documentation for i18n-1.10.0
Parsing documentation for activesupport-6.1.6
Installing ri documentation for activesupport-6.1.6
Parsing documentation for cocoapods-core-1.11.3
Installing ri documentation for cocoapods-core-1.11.3
Parsing documentation for cocoapods-1.11.3
Installing ri documentation for cocoapods-1.11.3
Done installing documentation for nanaimo, colored2, claide, CFPropertyList, atomos, xcodeproj, ruby-macho, nap, molinillo, gh_inspector, fourflusher, escape, cocoapods-try, netrc, cocoapods-trunk, cocoapods-search, cocoapods-plugins, cocoapods-downloader, cocoapods-deintegrate, ffi, ethon, typhoeus, public_suffix, fuzzy_match, concurrent-ruby, httpclient, algoliasearch, addressable, zeitwerk, tzinfo, i18n, activesupport, cocoapods-core, cocoapods after 17 seconds
34 gems installed
 cityfruit@shiyanchaodeMBP  ~  
 cityfruit@shiyanchaodeMBP  ~  pod --version
1.11.3
 cityfruit@shiyanchaodeMBP  ~  

使用

鸣谢

上一篇下一篇

猜你喜欢

热点阅读