Cocoapods Installing and using notes by STP

Source: Internet
Author: User
Tags amazon cloud services

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

cocoapods can be easily installed via the rubygems of your Mac.
Open terminal, and then type the following command:
 sudo gem install  cocoa pods

Enter a native password

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:cocoa Pods


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 source for 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 cocoa pods

Enter your native password and wait a few minutes patiently


There should be no problem.

Iv. Use of Cocoapods

Enter the directory where the project is located (project root directory)
Execute command

Vim 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)

Keyboard input I, enter edit mode, enter

Platform:ios, ' 7.0 '
Pod ' Mbprogresshud ', ' ~> 0.9.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.

Then press ESC and enter the ":" Sign into the VIM command mode and enter WQ after the colon

For example:
I want to integrate mbprogresshud This library class, you need to search the cocoapods first whether there is a need for the library, You can enter in Terminal:
pod search mbprogresshud
Enter to see some library classes related to the keywords you searched for.


The first one is what we need, the pod ' mbprogresshud ", ' ~>0.9.2 '
The line is copied to our Podfile file, save the changes.

pod install

At this point, you may encounter the following error:


Solution:

Terminal input: sudo xcode-select-switch/applications/xcode.app (your Xcode installation path)

Note: There are no spaces between xcode-select, there are spaces before-switch


So, mbprogresshud has been downloaded and set up the compilation parameters and dependencies, in the future when using the following two points:
1. You will need to use the cocoapods generated   . xcworkspace file to open the project instead of using the previous . xcodeproj file, as shown in:


2. Each time you change the Podfile file, you need to re-execute the pod update command

3. To import the corresponding header files when using: #import "MBProgressHUD.h"


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.


Cocoapods Installing and using notes by STP

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.