When we develop mobile applications, we generally use third-party tools, and because of the wide variety of third-party libraries, we manage the project in a relatively cumbersome way, so at this point we need a package management tool, in iOS development, we use the most is cocoapods.
Cocoa is an API for developing iOS touch-class devices, which is developed under OS X Apple's Api,cocoa touch. For example, in developing iOS, we often need to create a new class that is created with the Cocoa Touch class. In this blog, we will discuss the installation and use of the package management tool Cocoapods.
Installation of "one" cocoapods
(1) Cocoapods's official website is: https://cocoapods.org/. If your computer already has a ruby development environment installed, you can install it directly in the terminal (Terminal) using the following command:
- sudo gem install Cocoapods
(2) If your computer does not have a Ruby environment installed, take a look at the following steps: First install RVM, the Ruby virtual machine, similar to the JVM, execute the following command:
1. Curl-l Https://get.rvm.io | Bash-s Stable
(3) Reload the RVM, or re-open the terminal terminal can also:
- SOURCE ~/.RVM/SCRIPTS/RVM
(4) Check that the RVM is installed successfully and view the RVM version:
- Rvm-v
(5) Then use RVM to install the Ruby environment:
- RVM Install 2.0.0
(6) To view the version of Ruby:
- Ruby-v
(7) When installing Ruby, the gem is installed by default and we can also view the gem version:
- Ruby-v
(8) The following starts to install Cocoapods, because there is a wall, so we want to modify the image of Ruby, it happens that a treasure provides this image: First remove the original image:
- Gem Sources--remove https://rubygems.org/
If the gem is too old, you can try upgrading the gem using the following command:
$ sudo gem update--system
(9) Then add a new Image:
- Gem Sources-a http://ruby.taobao.org/
(10) To view the current Ruby image, if the display is taobao.org:
- Gem Sources-l
(11) This is the final step, install the Cocoapods: Wait a moment to it;
- sudo gem install Cocoapods
The use of "two" cocoapods
(1) First set up an iOS project, I take swift language as an example, I named Cocoapodsdemo. The current directory structure is as follows:
。
(2) Under the terminal to enter the root directory of this project, using Vim to create a podfile file, note that this name must be Podfile, can not be another name!!!
.
(3) then enter the following code in Podfile, where I take afnetworking as an example: Save exit.
Note: Here you can write directly:
Platform:ios
Pod ' afnetworking '
In other words, iOS can not add version number, iOS three letters must be lowercase, not written as "ios"!!!
There is no space between the colon and IOS!!!!!
。
(4) Then the following command is still executed at the root of the project: results
- Pod Install
。
Tip: If we need to view information about a third-party package, or if you are not sure about a package, you can use the following command:
Pod Search afnetworking
You can print out the following information:
(5) Then prompt in the terminal to use ***.xcworkspace to open the project, so we close the previous Xcode, to the project root directory, Open the Cocoapodsdemo.xcworkspace and then open the project, and the project structure has been turned into this:
。
(6) After each change of the Podfile file, the pod Update command needs to be executed.
When the pod install is performed, in addition to the Podfile, a Podfile.lock file is generated that locks the version of the current dependent library and does not change the version even if the pod install is executed more than once, only the pod Update will change Podfile.lock. This prevents third-party library upgrades from being inconsistent with each other's third-party library versions when collaborating with multiple people. So when you commit a version, you cannot drop it or add it to the. Gitignore.
After executing the above section, Cocoapod has been fully installed.
Cocoapod Installation-Manage third-party libraries