English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
First, what is CocoaPods
1Why do we need CocoaPods
When developing iOS, it is inevitable to use third-party open source libraries, such as SBJson, AFNetworking, Reachability, etc. When using these libraries, it usually requires:
Download the source code of the open source library and introduce it into the project
Add the framework used by the open source library to the project
Resolving dependency relationships between open source libraries and open source libraries, and between open source libraries and projects, checking for duplicate added frameworks, and other issues
When the open source library is updated, it is also necessary to delete the open source library used in the project, and then re-execute the previous three steps, and the head is big...
Since the advent of CocoaPods, these complicated tasks no longer require us to do it ourselves. We just need to do a small amount of configuration work, and CocoaPods will do everything for us!
2What is CocoaPods
CocoaPods is a tool to help us manage third-party dependency libraries. It can resolve dependency relationships between libraries, download the source code of libraries, and at the same time, create an Xcode workspace to connect these third-party libraries with our project for development use.
The purpose of using CocoaPods is to allow us to manage third-party open source libraries automatically, centrally, and intuitively.
Second, install CocoaPods
1Install
CocoaPods is implemented in Ruby, so in order to use it, you first need to have a Ruby environment. Fortunately, the default OS X system can already run Ruby, so we just need to execute the following command:
$ sudo gem install cocoapods
CocoaPods is installed as a Ruby gem package. During the installation process, it may ask us whether to update rake, just enter y. This is because the rake gem package will check more carefully during the installation process, and if there is a new version available, the option mentioned earlier will appear.
At the end of the installation process, execute the command:
$ pod setup
If there are no errors, it means that the installation has been successful!
2Problems that may be encountered during installation
①The install command does not respond for half a day after execution
This may be because the default source of Ruby uses cocoapods.org, and sometimes there may be problems accessing this website in China. One solution on the Internet is to replace it with Taobao's, and the replacement method is as follows:
$ gem sources --remove https://rubygems.org/ //After they respond, type the following command $ gem sources -a http://ruby.taobao.org/
To verify whether the replacement has been successful, you can execute:
$ gem sources -l
The normal output is:
*** CURRENT SOURCES *** http://ruby.taobao.org/
②The gem version is too old
gem is the standard package for managing Ruby libraries and programs. If its version is too low, it may also cause installation failure. The solution is naturally to upgrade gem and execute the following command:
$ sudo gem update --system
After the installation is completed, an error occurs when executing the pod setup command:
/Users/wangzz/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:298:in `to_specs': Could not find 'cocoapods' (>= 0) among 6 total gem(s) (Gem::LoadError) from /Users/wangzz/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:309:in `to_spec' from /Users/wangzz/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_gem.rb:53:in `gem' from /Users/wangzz/.rvm/rubies/ruby-1.9.3-p448/bin/pod:22:in `<main>'
This is a path setting issue, which can be resolved by executing:
$ rvm use ruby-1.9.3-p448
Solve this problem.
3Upgrade CocoaPods
The upgrade is very simple, just execute the installation command again:
$ sudo gem install cocoapods
It should be noted that if sudo was used during installation, the same keyword must be used during the upgrade. Otherwise, the path mismatch problem may occur again after the upgrade is completed.
Third, using CocoaPods
If everything goes well before, then you can experience the magic of CocoaPods, which needs to go through the following steps:
To demonstrate this process, I created a project named CocoaPodsTest.
1、create Podfile
Everything in CocoaPods starts with a file named Podfile, and we need to create this file first. It is a personal habit to use the command line, and I will do it like this:
$ cd /Users/wangzz/Desktop/CocoaPodsTest $ touch Podfile
Firstly, enter the root directory of the project, create an empty Podfile file, and the directory structure after creation is shown in the following figure:
(PS:The Podfile file can also be placed outside the root directory of the project, which will be a bit more麻烦, and it will be introduced in the next article, please pay attention.)
2、edit Podfile
According to the needs, we can write the required third-party libraries in the Podfile file. Taking SBJson, AFNetworking, and Reachability as examples, the content of my Podfile is as follows:
platform :ios pod 'Reachability', '~> 3.0.0' pod 'SBJson', '~> 4.0.0' platform :ios, '7.0' pod 'AFNetworking', '~> 2.0'
3、execute the import command
After all the preparations are completed, start to import third-party libraries:
$ cd /Users/wangzz/Desktop/CocoaPodsTest $ pod install
First, enter the root directory of the project, then execute the pod install command, and CocoaPods will start to do a series of work for us, such as downloading source code, configuring dependencies, and introducing the required frameworks, and the execution result of the command is printed as follows:
Analyzing dependencies Downloading dependencies Installing AFNetworking (2.1.0) Installing JSONKit (1.5pre) Installing Reachability (3.0.0) Generating Pods project Integrating client project [!] From now on use `CocoaPodsTest.xcworkspace`.
This indicates that the pod install command was executed successfully. Let's take a look at the changes in the root directory of the project as shown in the following figure:
It can be seen that three items have been added under the root directory of the project: CocoaPodsTest.xcworkspace, Podfile.lock file, and Pods directory.
(PS: Due to space limitations, the Podfile.lock file will be introduced in the next article in the series. Please stay tuned.)
Let's take a look at the last line of the content printed out after executing the pod install command just now:
[!] From now on use `CocoaPodsTest.xcworkspace`.
Prompting us to start using the CocoaPodsTest.xcworkspace file for development from now on.
There are a few points to note about the changes to the project:
Third-party libraries will be compiled into static libraries for our actual project to use
CocoaPods will organize all third-party libraries into a project named Pods in the form of targets, which is placed in the newly generated Pods directory. The entire third-party library project will generate a static library named libPods.a to provide for our own CocoaPodsTest project.
Our project and the project where the third-party libraries are located will be managed by a newly generated workspace
To facilitate our intuitive management of the project and third-party libraries, the CocoaPodsTest project and Pods project will be organized and managed in the form of a workspace, that is, the CocoaPodsTest.xcworkspace file we saw just now.
The original project settings have been changed. At this time, if we directly open the original project file to compile, it will report an error, and we can only use the newly generated workspace for project management.
Open CocoaPodsTest.xcworkspace, the interface is as follows:
The directory structure of the project is still very obvious.
To reference the header files of the third-party libraries added just now in the project, perform the compilation operation, and it will be successful!
Thus, the CocoaPods usage section comes to an end. Next, I plan to introduce something slightly more advanced about CocoaPods in another article. Please stay tuned.
Section 4: References
1http://code4app.com/article/cocoapods-install-usage
2http://cocoapods.org/