table of Contents
- CocoaPods installation
- CocoaPods use
- Introduction to CocoaPods principle
1. CocoaPods installation
CocoaPods using the Ruby scripting language, so we need to have Ruby Mac environment. The Mac is equipped with a built-Ruby, but lower than the regular version, we need to upgrade to a newer version.
Upgrading Ruby
we need to use its version management tools RVM
, Therefore, we need to install it first RVM
. The installation RVM
process will automatically Homebrew
install the dependency package, which Homebrew
is a software package management tool under the macOS platform.
So our entire installation sequence is Homebrew
~> RVM
~> Ruby
.
Next we will execute a series of installers in the Mac terminal.
You
may need to enter a password during the installation of Homebrew . Note that the cursor does not respond when you enter the password.
Drink a cup ☕️, this process is a bit long...
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
View version
brew -v
Install RVM
curl -L get.rvm.io | bash -s stable
Check the version (it may be prompted to reopen the terminal or open another one)
rvm -v
Upgrade Ruby to
view the current local Ruby version
rvm list
List all known Ruby versions
rvm list known
Choose the latest installation. Have a drink ☕️, this step is also time-consuming...
rvm install 2.7.0
Specify the version just installed as the system default version
rvm use 2.7.0 --default
Replace Ruby default source
Ruby default source is not accessible in China, so you need to replace the Ruby mirror. The gem command will be used, gem is a standard package for managing Ruby libraries and programs.
Remove the system default source
gem sources --remove https://rubygems.org/
Add new source
gem source -a https://gems.ruby-china.com
Check whether the replacement is successful
gem sources -l
Install CocoaPods
sudo gem install -n /usr/local/bin cocoapods
Update the index files of all third-party libraries to the local directory
pod setup
Then search for a library, if you don't execute it pod setup
, after searching: wq exit.
pod search FMDB
Query version
pod --version
At this point, CocoaPods is installed.
2. Use of CocoaPods
cd to the project directory:
cd /Users/kang/Desktop/KKPodsDemo
Create a Podfile file:
touch Podfile
Use vim to edit the file, or double-click the file to open it for editing.
vim Podfile
Type i to enter the input mode:
[光标]
~
~
~
"Podfile" 0L, 0C
Pod third-party library:
platform :ios, '9.0'
target `KKPodsDemo` do
pod 'AFNetworking'
pod 'FMDB'
end
Then press to Esc
exit the input mode, then :wq
save and exit.
Then execute the update operation and wait for the third-party library to be downloaded:
pod install
If you need to add a library later, you only need to perform an update operation:
pod update
After completion, there will be an additional xcworkspace:
Then we use this workspace to open the project, and we can see that the third-party library has been loaded:
3. Introduction to CocoaPods principle
We know that the implementation of End pod install
after, our project directory xcworkspace more than a workspace and project Pods. Pods third-party libraries are placed to manage the project, which will be compiled into third-party libraries .a
or .framework
library project calls for Pods, and Pods will generate a libPods-XXX.a
library for our original engineering project. As shown in the figure:
Open the TARGET of KKPodsDemo, click Build Settings, search and search
view the project header file reference:
We see that the header files of the third-party library are referenced, and these header files have been copied in the folder under the Pods project:
This way we can directly reference third-party libraries in our project:
#import "AFNetworking.h"
#import "FMDB.h"
Of course, here is just a brief discussion on the implementation principle of CocoaPods, and will not continue to explore more details (such as system dependencies and compilation parameters, etc.).