The latest Mac OS X 10.12.1 cocoapods installation and Usage Details, 10.12.1cocoapods
Cocoapods Official Website: https://cocoapods.org
1. What is CocoaPods?
When each language develops to a stage, corresponding dependency management tools, such as Maven in Java and npm in nodejs, will appear. With the increasing number of iOS developers, the industry has also seen a tool that provides dependency management for iOS programs. Its name is CocoaPods.
The source code of the CocoaPods project is managed on Github. This project started in August 12, 2011. After years of development, it has now become a de facto dependency management standard tool for iOS development. Third-party open-source libraries are inevitable for iOS projects. The emergence of CocoaPods allows us to save time to set and update third-party open-source libraries.
Ii. Why CocoaPods?
Before using CocoaPods, we need
1. Copy the source code of the open source Library to the project.
2. Add some dependency frameworks and dynamic libraries
3. Set parameters such as-ObjC and-fno-objc-arc.
4. manage their updates
After using CocoaPods, we only need to put the used open source library into a file named Podfile, and then execute pod install, cocoapods automatically downloads the source code of these third-party open-source libraries and sets the system dependencies and compilation parameters for our projects.
Iii. CocoaPods principles
The principle of CocoaPods is to place all the dependent libraries in another project named Pods, and then let the main project depend on the Pods project. In this way, the source code management work is moved from the main project to the Pods project. The Pods project is eventually compiled into a file named libPods. a. The main project only needs to rely on this. a file.
Iv. CocoaPods Installation
CocoaPods can be conveniently installed through RubyGems of Mac.
Start Terminal (the Terminal that comes with the Mac computer) and follow the instructions below:
1. Set the ruby software source
This is because ruby's software source rubygems.org was blocked by tianchao because of Amazon's cloud services. I need to update the ruby source as follows:
Gem sources-l # (view the current ruby source)
Gem sources -- remove https://rubygems.org/# (remove the source of the current ruby)
Gem sources-a https://ruby.taobao.org/# (set the current ruby source to my day)
Gem sources-l # (view the current ruby source again)
If Terminal output:
* ** Current sources ***
Https://ruby.taobao.org/
This proves that the ruby software source has been set up.
2. Set gem to the latest version.
If gem is too old, you can try to use the following command to upgrade gem:
Enter the following command in Terminal:
Sudo gem update -- system
After the upgrade is successful, the system prompts: Latest version currently installed. Aborting.
3. Run the CocoaPods installation command.
Note: The OS X 10.11 upgrade, although officially claimed to be a small upgrade, but for developers, cocoapods needs to be re-installed, but according to the previous installation method, enter the following command in Terminal:
Sudo gem install cocoapods
If the following error is reported:
ERROR: While executing gem... (Errno: EPERM)
Operation not permitted-/usr/bin/xcodeproj
Solution 1:
Run the following command and restart
Sudo nvram boot-args = "rootless = 0"
Sudo reboot
Run this command to check the restart.
Sudo gem install cocoapods-V
If there are still errors, use the second solution
Solution 2:
Sudo gem install-n/usr/local/bin cocoapods
Pod setup
Note that the Setting up CocoaPods master repo will be output during pod setup execution, but it will take a long time. In this step, Cocoapods is downloading its information ~ /. In the cocoapods directory, if you have waited for too long, you can try cd to that directory and use du-sh * to view the download progress.
After the installation is successful, you will see: Setup completed
5. Use of Cocoapods
Create a file named Podfile in one way and put it in the root directory of your project. (You can create a file in the root directory of the project by yourself)
The content format of the Podfile file should be as follows:
Platform: ios, '8. 0' # (indicating your development platform and version. If '8. 0' is left blank, it is the latest version)
Pod 'afnetworking', '~> 2.5.3 '# (' ~> 2.5.3 'indicates the version number. If this parameter is left blank, the latest version is used)
Pod 'sdwebimage', '~> 3.7.2'
Then run the following command in the Terminal to enter the root directory (Project root directory) of the project:
Pod install
In this way, AFNetworking and SDWebImage have been downloaded and the compilation parameters and dependencies have been set. Remember the following two points for future use:
1. From now on, you need to use the. xcworkspace file generated by Cocoapods to open the project, instead of using the previous. xcodeproj file.
2. Each time the Podfile file is changed, the pod update command needs to be re-executed.
Find third-party libraries:
If you do not know whether the database managed by cocoaPods has the library you want, you can search through the pod search Command, the following are all available libraries I found using pod search json:
You can enter the following in Terminal:
Pod search AFNetworking
After you press enter, you can see some library classes related to the keywords you search,
About Podfile. lock
After you run pod install, CocoaPods generates a file named Podfile in addition to Podfile. lock file, Podfile. lock should be added to version control and should not be added to this file. in gitignore. Because Podfile. lock locks the current version of each dependent database. If you execute pod install multiple times, the version will not be changed. Podfile. lock will be changed only after pod update. In this way, when multiple users collaborate, the versions of third-party libraries may be different when the third-party libraries are upgraded.
Appendix: How to Use the CocoaPods image index:
Podspec files for all projects are hosted in the https://github.com/CocoaPods/Specs,
The first time you run pod setup, CocoaPods updates these podspec index files to the local ~ /. Under the cocoapods directory,
This index file is large, so the first update is very slow.
Friendly people have created Cocoapods index library images on servers in China,
Therefore, it is much faster to execute indexes and new operations. The specific operations are as follows:
Pod repo remove master
Pod repo add master https://gitcafe.com/akuandev/Specs.git
Pod repo update
This is to use the image on gitcafe and place the https://gitcafe.com/akuandev/Specs.git in the above Code
Replace with the http://git.oschina.net/akuandev/Specs.git to use the image on oschina.