IOS CocoaPods usage

Ever since the CocoaPods, these complex work is no longer needed our hands, and we just need to do a small amount of configuration work, CocoaPods will do everything for us
 

First, what is CocoaPods

1. Why do we need CocoaPods
making iOS development time, always found the use of third-party open source libraries, such as SBJson, AFNetworking, Reachability and so on. When using these libraries typically leads to:

Download the open source library source code and introduce the project
to add open source library to use the framework to project
resolve dependencies between open source libraries and open source libraries and open source libraries and engineering, inspection framework duplicate add other issues
if there is time to update open source libraries , also need to delete the project using open source library, re-execute the previous three steps, suddenly have a big head. . .
Ever since the CocoaPods, these complex work is no longer needed our hands, and we just need to do a small amount of configuration work, CocoaPods will do everything for us!

2. What is CocoaPods
CocoaPods is a tool to help us manage third-party dependent libraries. It can resolve dependencies between the library and the library, download the source code for the library, at the same time to connect a Xcode workspace by creating these third-party libraries and our project together, we have developed for use.

Use CocoaPods purpose is to allow us to automated, centralized, intuitive management of third-party open source libraries.

Second, the installation CocoaPods

1, the installation
CocoaPods is implemented in Ruby, in order to use it you first need to have Ruby environment. Fortunately, OS X system default has to run Ruby, so we just need to execute the following command:

?
1
$ sudo gem install cocoapods

CocoaPods in the form of Ruby gem package is installed. During the installation of execution, we might ask is not updated rake, you can enter y. This is because the rake gem bag checks finer in the installation process, if a new version is available just options will appear.
At the end of the installation process, execute the command:

?
1
$ pod setup

If there is no error, it shows all the installation is successful!
2, problems may be encountered during installation
① complete the install command for a long time did not respond
This may be because the default source Ruby is used cocoapods.org, domestic accessing the site sometimes be a problem, a solution online is replaced by the far Taobao, replace as follows:

?
1
2
3
$ gem sources --remove https: //rubygems.org/
//等有反应之后再敲入以下命令
$ gem sources -a http: //ruby.taobao.org/

To verify that the replacement is successful, you can do:

?
1
$ gem sources -l

Normal output is:

?
1
2
3
*** CURRENT SOURCES ***
  
http: //ruby.taobao.org/

②gem version is too old
gem is Ruby libraries and standard package management program, if its version is too low can also cause the installation to fail, the solution is to upgrade natural gem, execute the following command:

?
1
$ sudo gem update --system

③ After installation, when executed pod setup command being given:

?
1
2
3
4
/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 the problem path set by executing:

?
1
$ rvm use ruby- 1.9 . 3 -p448

To solve the problem.
3, upgrade CocoaPods
upgrade is very simple, you can perform the installation command again:

?
1
$ sudo gem install cocoapods

Note that, if installed when using sudo, like the need to upgrade when using this keyword will appear otherwise upgrade path later finished mismatch.


Third, the use CocoaPods
if done before all goes well, then you can experience the magic of CocoaPods of experience, the need to go through the following steps:

To illustrate this process, I created a project called CocoaPodsTest of.

1. Create Podfile
all CocoaPods is a file named Podfile from the beginning, we need to create this file. Personal habits using the command line, I will do so:

?
1
2
$ cd /Users/wangzz/Desktop/CocoaPodsTest
$ touch Podfile

First into the root directory of the project, create a blank Podfile file is created in the directory structure shown below:

(PS: Podfile file may not be placed in the root directory of the project, but will a little trouble, will be introduced next article, so stay tuned.)

2, edit Podfile
if necessary, we can write files in Podfile need to use third-party libraries to SBJson, AFNetworking, Reachability three libraries, for example, my Podfile reads as follows:

?
1
2
3
4
5
6
platform :ios
pod 'Reachability' , '~> 3.0.0'
pod 'SBJson' , '~> 4.0.0'
 
platform :ios, '7.0'
pod 'AFNetworking' , '~> 2.0'

3, the import command
after preparations are completed, began to import third-party libraries:

?
1
2
$ cd /Users/wangzz/Desktop/CocoaPodsTest
$ pod install

First to enter the project root directory, and then execute pod install command, CocoaPods began to do for us to download the source code, configuration dependencies, the introduction of framework and some other work required to print out the results of the command as follows:

?
1
2
3
4
5
6
7
8
9
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 explains pod install command successful. Look at the changes in the project root occur, as shown below:

We can see, more than three things in the root directory of the project: CocoaPodsTest.xcworkspace, Podfile.lock Pods files and directories.

(PS: limited space, Podfile.lock file will be put under a series of reports, so stay tuned.)

Look at what was just after executing pod install command to print out the last line:

?
1
[!] From now on use `CocoaPodsTest.xcworkspace`.

提示我们从现在起,我们需要使用CocoaPodsTest.xcworkspace文件来开发。
对于工程发生的变化,有几点需要说明:

第三方库会被编译成静态库供我们正真的工程使用


CocoaPods会将所有的第三方库以target的方式组成一个名为Pods的工程,该工程就放在刚才新生成的Pods目录下。整个第三方库工程会生成一个名称为libPods.a的静态库提供给我们自己的CocoaPodsTest工程使用。

我们的工程和第三方库所在的工程会由一个新生成的workspace管理
为了方便我们直观的管理工程和第三方库,CocoaPodsTest工程和Pods工程会被以workspace的形式组织和管理,也就是我们刚才看到的CocoaPodsTest.xcworkspace文件。

 

原来的工程设置已经被更改了,这时候我们直接打开原来的工程文件去编译就会报错,只能使用新生成的workspace来进行项目管理。

打开CocoaPodsTest.xcworkspace,界面如下:

工程的目录结构还是非常明显的。

在项目中引用刚才添加的第三方库的头文件,执行编译操作,果断成功!
至此,CocoaPods使用篇告一段落,接下来打算再用一个篇幅介绍CocoaPods稍微深层次的东西,敬请关注。

四、参考资料

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

Guess you like

Origin www.cnblogs.com/itgezhu/p/11087596.html