IOS Cocoapods Detailed use method _ios

Source: Internet
Author: User
Tags install cocoapods

One, what is Cocoapods

1. Why need Cocoapods
In the development of iOS, there is always the use of Third-party open source libraries, such as Sbjson, afnetworking, reachability and so on. When using these libraries, you typically need to:

Download source code for Open-source Library and introduce engineering
To add the framework used by the open Source Library to the project
Resolve dependencies between open source and open source libraries and open source libraries and projects, check for overlapping frameworks, and so on
If the Open Source Library has an update, you need to remove the open source library used in the project, and then perform the previous three steps, suddenly the head is big ...
Since the cocoapods, these complicated work no longer need our pro-Pro, just need us to do a small amount of configuration work, Cocoapods will do everything for us!

2, what is Cocoapods
Cocoapods is a tool used to help us manage a third-party dependency library. It can solve the dependencies between library and library, download the source code of the library, and connect these third-party libraries with our project by creating a Xcode workspace for us to develop and use.

The purpose of using cocoapods is to enable us to automate, centralize, and visualize the management of third party open source libraries.

Second, install Cocoapods

1, installation
Cocoapods is implemented in Ruby, which requires a Ruby environment to use first. Luckily, the OS X system is already running Ruby by default, so we just need to execute the following command:

 
 

Cocoapods is installed in the form of a Ruby gem package. During the installation execution, you may be asked if we are updating rake, and we can enter Y. This is because the Rake gem package will check for finer detail during installation, and if a new version is available, the option will appear earlier.
At the end of the installation process, execute the command:

$ pod Setup

If there is no error, it means that all the installation was successful!
2, the installation process may encounter problems
① executed the install command, half a day, no response.
This may be because Ruby's default source is using cocoapods.org, domestic access to the Web site is sometimes problematic, one of the online solution is to replace the Cheng treasure, the replacement way is as follows:

$ gem Sources--remove https://rubygems.org/ 
//etc after the reaction and then typing the following command 

To verify that the substitution succeeded, you can perform:

 
 

The normal output is:

Current SOURCES * * * 
 

②gem version too old
A gem is a standard package for managing Ruby libraries and programs, and if its version is too low and may cause installation to fail, the solution is naturally to upgrade the gem, and execute the following command:

 
 

③ after installation completes, the Pod Setup command times is wrong:

/users/wangzz/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:298:in ' to_specs ': Could ' 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 the problem with path setting, which can be done by:

 
 

Solve the problem.
3, Upgrade Cocoapods
The upgrade is simple, and the install command can be executed again:

 
 

It should be noted that if the installation of the use of sudo, upgrade when the same need to use the keyword, or the upgrade will be completed after the path mismatch problem.


iii. use of Cocoapods
If everything goes well, then you can experience the wonders of cocoapods, and you need to go through the following steps:

To demonstrate this process, I created a project called Cocoapodstest.

1. Create Podfile
Cocoapods everything starts with a file called Podfile, and we need to create this file first. Personal habits using the command line, I would do this:

$ cd/users/wangzz/desktop/cocoapodstest
$ touch podfile

First go to the root of the project, create a blank podfile file, create a complete directory structure as shown below:

(Ps:podfile file can also not be placed in the root of the project, just a little trouble, the next article will be introduced, please pay attention to.) )

2. Edit Podfile
As needed, we can write a third-party library that needs to be used in the Podfile file, taking Sbjson, Afnetworking, reachability three libraries as an example, and my podfile contents are as follows:

Platform:ios
pod ' reachability ', ' ~> 3.0.0 '
pod ' Sbjson ', ' ~> 4.0.0 '

platform:ios, ' 7.0 '
pod ' Afnetworking ', ' ~> 2.0 '

3. Execute Import command
When the preparations are complete, start importing the Third-party libraries:

$ cd/users/wangzz/desktop/cocoapodstest
$ pod Install

First into the engineering root directory, and then execute the pod Install command, Cocoapods began to do for us to download the source code, configure dependencies, the introduction of the required framework, and other columns of work, the results of the execution of the command 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

[!] ' Cocoapodstest.xcworkspace ' with ' from '.

This means that the pod install command was executed successfully. Take a look at the changes in the engineering root directory as shown below:

As you can see, there are three more things in the root of the project: Cocoapodstest.xcworkspace, Podfile.lock files, and pods directories.

PS: Space is limited, Podfile.lock file will be placed in a series of articles next introduction, please pay attention to. )

And look at the last line of what the pod Install command has just done:

[!] ' Cocoapodstest.xcworkspace ' with ' from '.

Prompts us from now on, we need to use the Cocoapodstest.xcworkspace file to develop.
For the changes in the project, there are several points to explain:

The third party library will be compiled into a static library for our true engineering use


Cocoapods will have all the Third-party libraries in target form a project called pods, which will be placed under the newly generated pods directory. The entire third party library project will generate a static library called LIBPODS.A to provide our own Cocoapodstest engineering use.

Our project and the third Third-party project will be managed by a newly generated workspace
In order to facilitate our intuitive management of engineering and Third-party libraries, Cocoapodstest Engineering and Pods engineering will be organized and managed in the form of workspace, the Cocoapodstest.xcworkspace file we just saw.

The original project settings have been changed, at this time we directly open the original project files to compile will be an error, 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.

In the project reference just added the third party library header file, perform the compile operation, decisive success!
At this point, cocoapods use article to come to an stop, and then intend to use a space to introduce cocoapods slightly deeper things, please pay attention to.

IV. Reference materials

1, Http://code4app.com/article/cocoapods-install-usage
2, http://cocoapods.org/

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.