Original: http://www.jianshu.com/p/6e5c0f78200a
First, what is Cocoapods
Cocoapods is a dependency management tool for iOS projects, and the project source code is managed on GitHub. Developing iOS projects inevitably uses third-party open source libraries, and the advent of cocoapods allows us to save time setting up and third-party open source libraries.
Before using Cocoapods, the development project needs to use the third party open Source Library, we need
1. Copy the source code to the project
2. Add some dependent frameworks and dynamic libraries
3. Set parameters such as-objc,-fno-objc-arc
4. Manage their updates
After using Cocoapods, we just need to put the open Source library into a file named Podfile, and then execute the pod install. Cocoapods will automatically download the source code for these third-party open source repositories, and set the system dependencies and compilation parameters for our project to be responsive.
Second, the principle of cocoapods
The cocoapods principle is that all dependent libraries are placed in another project called pods, and then the master project is dependent on the pods project, so that the source management work is moved from the main project to the Pods project. The PODS project is eventually compiled into a file called LIBPODS.A, and the main project only needs to rely on this. a file.
Third, the installation of Cocoapods
The cocoapods can be easily installed via the rubygems of the Mac itself.
Open terminal, and then type the following command:
$ sudo gem install Cocoapods
Execute this sentence if the following error is reported:
Error:could not find a valid gem ' cocoapods ' (>= 0), where is:
Unable to download data from https://rubygems.org/-errno::etimedout:operation timed Out-connect (2) (Https://rubygems. org/latest_specs.4.8.gz)
Error:possible Alternatives:cocoapods
This is because Ruby's software source rubygems.org because of the use of Amazon cloud services, was blocked by my celestial, need to update the source of Ruby, the process is as follows:
$ gem sources-l (view the source of the current ruby)
$ gem Sources--remove https://rubygems.org/
$ gem sources-a https://ruby.taobao.org/
$ gem sources-l
If the gem is too old, you can try upgrading the gem with the following command
$ sudo gem update--system
After successful upgrade, you will be prompted: RubyGems system software updated
Then re-execute the install download command
$ sudo gem install Cocoapods
There should be no problem.
Next install, execute:
$ pod Setup
Terminal will stay in Setting up CocoaPods master repo This state for some time, because the download installation, and the directory is relatively large, you need to wait patiently. If you want to speed up, You can use a mirrored index of cocoapods. (The method of using mirror index is attached at the end of the article)
After the installation is successful, you will see:
01.png
Iv. Use of Cocoapods
Enter the directory where the project is located (project root directory)
Execute Command Touch podfile
This is said to create a new file named Podfile (can not be written in other names, but also in the project root directly in the new)
Then edit the file and execute the command open-e podfile
The first time you execute this command, there will be a blank file open, which you can put aside first,
The format of the Podfile file should be as follows:
Platform:ios, ' 7.0 '
Pod ' Amap2dmap ', ' ~> 2.5.0 '
Pod ' afnetworking ', ' ~> 2.5.3 '
Pod ' sdwebimage ', ' ~> 3.7.2 '
Some points to note: platform that line, iOS three letters are lowercase, and with the preceding colon can not be spaced, the subsequent version number can not be written, but some open source library for the version is required, such as to be more than 6.0 to run, encountered such an open source library need to write the version number.
Platform below is the open source library that Cocoapods needs to integrate and determine the integration of those libraries according to your needs.
As an example:
I want to integrate afnetworking this library class, need to search in the Cocoapods first whether the need library, you can enter in the terminal:
Pod Search afnetworking
After you enter, you can see some library classes related to the keywords you searched for,
02.png
The first one is what we need, the pod ' afnetworking ', ' ~>2.5.3 '
The line is copied to our Podfile file, saving the changes.
Then execute in Terminal:
Pod Install
In this way, Afnetworking has been downloaded and set up the compilation parameters and dependencies, in the future when using the following two points to remember:
1. From then on, you will need to use the . xcworkspace file generated by Cocoapods to open the project instead of using the previous . xcodeproj file
2. Each time you change the Podfile file, you need to re-execute the pod update command
PS: When performing pod install, in addition to Podfile, a file named Podfile.lock is generated, which locks the version of the current dependent library, and does not change the version even if the pod install is executed more than once, only the pod Update will change the Podfile.lock. This prevents third-party library upgrades from being inconsistent with each other's third-party library versions when multiple people collaborate. So when you commit a version, you cannot drop it or add it to the. Gitignore.
Five, frequently asked questions
1.
[!] Invalid Podfile
file:undefined local variable or method ' en_US ' for #<pod::P odfile:0x00000102a5d8b0>. Updating CocoaPods might fix the issue.
Cause: Single-quote format, which may be caused by manual input
Workaround: System Preferences-keyboard-text-uncheck the "Use smart quotes and dashes" option-and then modify the single (double) quotation mark inside the Podfile
2.argumenterror-invalid byte sequence in Us-ascii
Cause: Character Set error
Workaround:
Use the locale command to view the current character set, and if it is zh, you need to execute the following command:
Export Lc_all=en_us. UTF-8
Export. UTF-8
And then using the locale command to view it, it's changed.
[!] The YMTea [Debug]
target overrides the OTHER_LDFLAGS
build setting defined in ' Pods/target support Files/pods/pods.debug.xcconfig '. This can leads to problems with the CocoaPods installation
[!] The YMTea [Release]
target overrides the OTHER_LDFLAGS
build setting defined in ' Pods/target support Files/pods/pods.release.xcconfig '. This can leads to problems with the CocoaPods installation
-Use the "$ (inherited) ' flag, or-remove the build settings from the target.
Cause: I encountered this problem when I was integrating CocoaPods in an existing project because the project Target did some setup, and CocoaPods also made the default settings, which would cause problems if the two settings were inconsistent.
Workaround: I want to use the settings in CocoaPods, where I define and in my Project PODS_ROOT
Other Linker Flags
(Build settings), replace their values $(inherited)
, enter the terminal, perform pod update
The warning was gone, back to xcode,build through.
Another simple and rude way to do this online: Click on the project file project.xcodeproj, right button 显示包内容
, open with a text editor project.pbxproj
, delete OTHER_LDFLAGS
the place, save (this I have not tried)
4.
[!] Oh No, an error occurred.
It appears to the originated from your podfile on line 2.
Search for existing GITHUB issues similar to yours:
https://github.com/CocoaPods/CocoaPods/search?q=%2FUsers%2Fxiao6%2FMusic%2FGI06%E5%AE%9E%E8%AE%AD%E8%8A%B8%E8% 8c%97%e8%8c%b6%e5%8f%b6%2fymtea%2fpodfile%3a2%3a+syntax+error%2c+unexpected+%27%3a%27%2c+expecting+ End-of-input%0aplatform+%3a+ios%2c+%277.0%27%0a++++++++++%5e&type=issues
If none exists, create a ticket, with the template displayed above, on:
Https://github.com/CocoaPods/CocoaPods/issues/new
Be sure to first read the contributing guide for details on how to properly submit a ticket:
Https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md
Don ' t forget to anonymize any private data!
Reason: This problem compares egg ache, get a long time, look carefully to discover is because Podfile file inside platform that line colon and iOS more a space between .... In fact, this error in the wrong time Ruby has been given, but in the beginning did not look good:
03.png
Attached: How to use Cocoapods's mirrored index:
All project Podspec files are hosted in Https://github.com/CocoaPods/Specs, and the first time you execute pod setup, CocoaPods will update these podspec index files to local ~/. Cocoapods directory, this index file is relatively large, so the first update is very slow. Friendly people in the domestic server set up the image of the Cocoapods Index library, so the implementation of the index and new operations will be much faster. How to do this: $ pod repo Remove Master $ pod repo Add Master https://gitcafe.c Om/akuandev/specs.git $ pod Repo update this is using a mirror on Gitcafe to replace the https://gitcafe.com/akuandev/Specs.git in the above code with HTTP.// Git.oschina.net/akuandev/specs.git can use the mirror on the Oschina.
Recommended expand Reading
Cocoapods installation use and frequently asked questions