Installation and use of iOS package management tool Cocoapods

Source: Internet
Author: User
Tags install cocoapods

When we develop mobile applications, we generally use third-party tools, and because of the wide variety of third-party libraries, it is also relatively troublesome to manage them in our projects, so we need a package management tool at this point. In iOS development, the most we use 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.

"I. Installation of Cocoapods"

(1) Cocoapods's official site is: https://cocoapods.org/.

Let's say your computer already has a Ruby development environment installed. Then use the following command in the terminal (Terminal) to install it directly:

sudo gem install Cocoapods

(2) Assuming your computer does not have a Ruby environment installed, take a look at the steps below: First install RVM, the Ruby virtual machine, similar to the JVM, and run the following command:

Curl-l Https://get.rvm.io | Bash-s Stable

(3) To load the RVM again, or to re-open the terminal terminal can also:

SOURCE ~/.RVM/SCRIPTS/RVM

(4) Check if the RVM is installed successfully and see the RVM version number:

Rvm-v

(5) Then use RVM to install the Ruby environment:

RVM Install 2.0.0

(6) To view the version number of Ruby:

Ruby-v

(7) When installing Ruby, the gem is installed by default and we are able to view the gem's version number:

Gem-v


(8) The following starts to install Cocoapods, because there is a wall, so we want to change the image of Ruby, it happens that a treasure provides this image: First remove the original image:

Gem Sources--remove https://rubygems.org/


Assuming the gem is too old, you can try upgrading the gem using the following command, for example:

$ sudo gem update--system


(9) Then add a new Image:

Gem Sources-a https://ruby.taobao.org/

(10) View the current Ruby image, assuming that the display is taobao.org, you can:

Gem Sources-l

(11) This is the last step. Install Cocoapods: Wait a moment to be able.

sudo gem install Cocoapods


(12) In the earlier version number, after running (11), Cocoapods is installed. You will also need to run the Setup command recently:

Pod Setup
End Cocoapods the installation is complete. However, in the actual installation process, there will be a variety of problems and anomalies, for the problem of the solution, please refer to "three. Troubleshooting exception issues during Installation"

"Two. Use of Cocoapods"

(1) First build an iOS project. I take swift language as an example, I named Cocoapodsdemo. The current folder structure is as follows:


(2) Enter the root folder of this project under terminal. Use Vim to create a podfile file, note that the name must be Podfile, not the other name!

!。

.


(3) then enter code such as the following in Podfile. I take afnetworking as an example: Save exit.

Note: It is possible to write directly here (this is the simplest notation):

Platform:ios

Pod ' afnetworking '


In other words, iOS can not add a version number, iOS three letters must be lowercase. Can't be written as "IOS"!

。!

Personal advice:

I'm used to specifying the version number behind iOS, and I'm generally set to ' 8.0 ' so I can fit multiple version numbers.

Then, in fact, each third-party framework has a version number that can be viewed through pod search * * * To see the historical version number (as I'll mention below). Personal advice is also best to specify a stable version number. This prevents you from always updating to the latest version when you run the pod Update command later (this is not necessary), and it also results in a non-uniform version number. Suppose you do not specify a version number for a third-party library, the default is the latest version number.

The following is a more complete podfile notation:




(4) then still run under the project root folder such as the following command: Result

Pod Install



Tip: Suppose we need to see information about a third-party package. Or if you're not sure about a package. Ability to use such as the following command:

Pod Search afnetworking

You can print out information such as the following:



(5) Then prompt in the terminal to use ***.xcworkspace to open the project, so we close the previous Xcode. Come to the project root folder and open Cocoapodsdemo.xcworkspace. Then you open the project, and the project structure has become the same:


(6) You need to run the pod Update command every time you change the Podfile file.

After you run the pod install. In addition to Podfile, a Podfile.lock file is generated. It locks the version number of the current dependent library, and does not change the version number even if the pod install is run multiple times. Only running Pod update will change podfile.lock. In multiplayer collaboration, this prevents third-party libraries from being upgraded, resulting in inconsistent third-party library version numbers. So you can't drop it when you commit a version number. Do not join in the. Gitignore.


"Three. Troubleshooting exception issues during Installation"

In particular, for command-line operations, various installation problems will always exist. I have the following list of the problems and solutions I have encountered, for everyone to participate in the test:

(1) Change folder permissions, the installation process may tell you that some folders do not have permission to write, and prompts you:

sudo chown-r $ (whoami): admin/usr/local

$ (WHOAMI) is the name of your computer. You can run it.


(2) Install the Ruby Environment Run command: RVM install 2.0.0. will tell you to install several things:

Installing required Packages:libtool, Libyaml, ReadLine, LIBKSBA

Then run the following command separately to:


Brew Install Libtol

Brew Install Libyaml

Brew Install ReadLine

Brew Install LIBKSBA

Let's say you want to install similar packages, and you can run them with this command.


(3) RVM list

The command is to query for Ruby that has already been installed, so that if Ruby is not installed, you will be able to run RVM Install 2.0.

RVM Rubies
# No RVM Rubies installed yet. Try ' RVM help install '.

(4) RVM Install 2.0.0

The following error occurred while installing Ruby:

Curl: (+) Server aborted the SSL handshake Error: Failed to download Resource "OpenSSL"

You may also receive issues such as the following:Curl: (sslread) return error-9806

I tested that the problem should not affect the installation. Temporary can be ignored.


(5) RVM list

After you install Ruby, you will have the following hints:

chenyufeng:~ $RVM LISTRVM rubies   ruby-2.0.0-p643 [x86_64]# Default Ruby not set. Try ' RVM alias create Default <ruby> '. # =-current# =*-current && default#  *-Default
It says you don't have the default Ruby set, which can be ignored and not set.


(6)sudo gem install cocoapods

When you run the install command. The following error may be reported:

ERROR: While  executing gem ... (errno::eperm)    Operation not permitted-/usr/bin/pod


This problem arises. Not to change permissions, the workaround is as follows:

sudo gem install-n/usr/local/bin cocoapods

sudo xcode-select--switch/applications/xcode.app

Finally, remember to run the command:

Pod Setup


(7) Installation afnetworking The latest version and 2.6.3 version number is not compatible.

Before my code was written on the basis of the AFNetworking2.6.3 version number. Later afnetworking upgrade to 3.0.0 after certain classes do not exist, based on this issue, please refer to the use of AFNetworking3.0.0 error resolution and suggestions for this blog.


(8) about the Cocoapods upgrade pit

Using Cocoapods now prompts us to upgrade, for example, as seen in:


It prompts you to upgrade using the "Gem install cocoapods" command. However, this command is often unable to complete the task successfully. will appear for example the following error:


So when upgrading, it is recommended to use the method in the above (6) to run:

sudo gem install-n/usr/local/bin cocoapods

After the upgrade is complete, it can be used normally.


(9) After upgrading to Cocoapods 1.0.1, there are also pits during use. Suppose we write the following Podfile file:

   Platform:ios, ' 8.0 '   pod ' afnetworking ', ' ~>2.6.3 '
Then run the pod install, after the command. There will be an error such as the following:

Indicates that the library does not specify a specific target. In our use of the old version of the cocoapods, this is not a problem, but in the current new cocoapods. For a random library. You need to specify the project targets, in general. This target is the project name. The correct podfile files are as follows:

Target "Showhiddenkeyboard" do   Platform:ios, ' 8.0 '   pod ' afnetworking ', ' ~>2.6.3 ' end

The Showhiddenkeyboard is my project name. Once this is complete, you can successfully use pod install to load the storage.


(10) This same is the Cocoapods post-upgrade pit. When I run the pod Update command such as the following:



Try multiple times. cannot be loaded successfully. Finally, you have to choose to use the Pod Install command only.


(11) This is a question about writing podfile files. Personal suggestions when loading a library, specify the corresponding version number.

Target "Showhiddenkeyboard" do   Platform:ios, ' 8.0 '   pod ' afnetworking ', ' ~>2.6.3 '   pod ' masonry ', ' ~> 1.0.1 ' End


As in the back of ' ~>2.6.3 '. It is also important to note that the pod command does not follow: colon. Otherwise you will get an error!


(12) Simply list the version number of the third-party library that I use frequently:

afnetworking:2.6.3

masonry:1.0.1


At this point, a third-party library has been imported into the project and can be referenced in development. In the future we will be able to use Cocoapods to manage our library, is it very convenient?


GitHub home: https://github.com/chenyufeng1991.

Welcome to visit us!


Installation and use of iOS package management tool Cocoapods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.