Article source: www.cnblogs.com/foxting/p/4…

Code4app.com/article/coc… \

My.oschina.net/u/1244672/b… \

rvm.io/rvm/install\

\

Install the RUBY environment on the MAC

Before installing CocoaPods, you need to configure the RUBY environment. This article summarizes how to install The RUBY environment. To install the Ruby environment, you need to install Xcode first, then Homebrew, then RVM, and finally the Ruby environment. You don’t have to install XCode, everyone can.

Homebrew is a package manager for installing UNiX tools on the MAC that are not available on OS X. Homebrew installs all these tools in the /usr/local/cellar directory and creates symbolic links in /usr/local/bin.

1. 英 文 address: Brew.sh /index_zh-cn…

Open Terminal and paste the following into Terminal

1 ruby -e ``"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After the installation is complete, you can check whether the installation is successful by using brew doctor command. If “Your system is ready to brew.” is displayed, you can use it. You can refer to: blog.csdn.net/maojudong/a…

2. Install and set RVM(need to climb over the wall, otherwise the server cannot be found, the installation fails)

Command: curl – L get. RVM. IO | bash – s stable

You will be asked for your administrator’s password and automatically install dependencies through Homebrew. After a while, the RVM will be successfully installed. Then, load the RVM environment (new Termal doesn’t need to do this, it will reload automatically). Run the following command to enable RVM source ~/. RVM /scripts/ RVM to check whether the commands are installed correctly

rvm -v 

Correct words like the following appear

RVM 1.26.11 (Master) by Wayne E. Seguin

, Michal Papis

[rvm. IO /] \
@gmail.com>
@gmail.com>

4. Install RUBY

rvm install ruby

After the installation is complete, you need to specify which RUBY is the default

rvm 2.? .? –default

There you have it, Ruby-V. If you want to install Cocoapods then go to the following link:

www.cnblogs.com/foxting/p/4…

 

Refer to the following links for the entire installation process:

www.2cto.com/kf/201411/3…

\

———————————————————————————————————————— ——————————————

CocoaPods installation

directory

  • What is CocoaPods?

  • How do I download and install CocoaPods?

  • How do I use CocoaPods?

    • Scenario 1: Import the AFNetworking library into your project using CocoaPods
    • Scenario 2: How do I correctly compile and run a project that contains the CocoPods class library

What is CocoaPods?

When you’re developing an iOS app, there are a lot of third party open source libraries like JSONKit, AFNetWorking, etc. Maybe one library uses other libraries, so to use it, you have to download other libraries, and other libraries use other libraries, and “the kids go on and on and on.” This might be a special case. Manually downloading the required libraries one by one is cumbersome. Another common situation is when the library you are using in your project is updated, and you have to download the new version and rejoin the project. It would be nice if there were a tool to solve these annoying problems. So, you need CocoaPods.

You can use CocoaPods to make sure that you set up your library correctly, as long as you use a single command. Importantly, most of the well-known open source libraries support CocoaPods. As iOS programmers, mastering CocoaPods is essential.

How do I download and install CocoaPods?

Before installing CocoaPods, first install the Ruby environment locally. As for how to install a Ruby environment on a Mac, please Google it and I won’t cover it in this article.

If you already have a Ruby environment installed locally, downloading and installing CocoaPods is as simple as a single command. Enter the following command in Terminator (note that all commands in this article are entered and run ina terminal). What? You don’t know what a terminal is? That please xiaobian dinner, xiaobian to tell you) :

sudo gem install cocoapods
Copy the code

But wait. If you are in China, you can type this command in the terminal and see nothing for half a day. For no other reason, because the wall is blocking Cocoapods.org. (Damn, walls, you ask! Yes, I wonder.)

But, yes, but (it’s a happy but). We can access Cocoapods using a Ruby image from Taobao. Type commands in the terminal in the following order:

$gem sources --remove https://rubygems.org/ $gem sources -a https://ruby.taobao.org/Copy the code

To verify that your Ruby image is and only taobao, use the following command:

$ gem sources -l
Copy the code

Your command is successful only if the following text appears in the terminal:

*** CURRENT SOURCES ***

https://ruby.taobao.org/
Copy the code

When all of the above commands are complete, the small editor terminal will look like this:

At this point, you run in the terminal again:

$ sudo gem install cocoapods
Copy the code

After a few seconds, CocoaPods will be downloaded and installed locally, with no setup required.

When the above command is typed, the small editor terminal looks like this (only the first part is cut because it is too long) :

See here, your heart will say, damn! It was so easy to download and install! Yes, xiaobian thinks so too. CocoPods are that simple and easy to use. Read on.

——————————————————————————————–

Run pod setup or pod install,

  1. If you want to use CocoaPods for the first time, run CocoaPods firstpod setup
  2. On the terminal, open the project directory and runpod install 
  3. pod installAfter the command is successfully executed, open the project and run the project through the newly generated xcworkspace file

Setting up CocoaPods master repo when pod setup is executed indicates that the CocoaPods are downloading its information to ~/.cocoapods.

Setting up CocoaPods master repo for a while

Run the du -sh * command to check the file size every few minutes. The final size of the directory is more than 100 MB.

——————————————————————————————–

How do I use CocoaPods?

Ok, with CocoPods installed, the next step is to use it. Fortunately, using CocoPods is as easy as installing it, and can be done with a line or two of command.

The editor uses two usage scenarios to illustrate how to use CocoaPods.

Scenario 1: Import the AFNetworking library into your project using CocoaPods

AFNetworking is available on GitHub at github.com/AFNetworkin…

To determine if AFNetworking supports CocoaPods, you can verify with CocoaPods’ search capabilities. Enter:

$ pod search AFNetworking
Copy the code

After a few seconds, you’ll see some information about the AFNetworking class library on the terminal. Such as:

AFNetworking supports CocoaPods, so you can use CocoaPods to import AFNetworking into your project.

First, we need to include CocoaPods support in our project. You can use Xcode to create a project named CocoaPodsDemo for this tutorial. Once created, before moving on, let’s take a snapshot of the project’s Xcode directory structure without CocoaPods support:

The figure above will be compared to the Xcode directory structure of the project after the project supports CocoaPods.

You may be wondering why CocoaPods can download AFNetworking instead of other libraries. The answer to this question is that there is a file that controls what CocoaPods downloads. This file is called an “Podfile” (note that it must be the name of the file and has no suffix). You create a Podfile and add the libraries you need to download to it, telling CocoaPods, “Get in the bowl!” . You only need one Podfile per project.

Well, without further ado, let’s create this amazing PodFile. In the terminal, go to the directory where your project is located. Then, in the current directory, create a Podfile using Vim and run:

$ vim Podfile
Copy the code

Then type the following text in your Podfile:

Platform :ios, '7.0' POD "AFNetworking", "~> 2.0"Copy the code

Note that this post was not created by the editor, but can be found on AFNetworking’s Github page. The latest version of iOS that AFNetworking supports is iOS 7.0, and the version you want to download is VERSION 2.0.

Then save and exit. In the VIM environment, the command for saving and exiting is:

:wq
Copy the code

In your project directory, you’ll find a file named Podfile with the same content you just typed. Note that Podfile should be in the same directory as your project file. Xcodeproj.

At this point, you can download the AFNetworking library using CocoPods. Again in the current project directory on the terminal, run the following command:

$ pod install
Copy the code

Because you’re importing AFNetworking into your project, that’s why this command requires you to run it in your project’s directory.

After running the above command, the following information appears on the xiaobian terminal:

EricmatoMacBook-Pro:CocoaPodsDemo ericwang$ pod install Analyzing dependencies Downloading dependencies Installing AFNetworking (2.0.2) Generating Pods Project Integrating Client project [!]  From now on use `CocoaPodsDemo.xcworkspace`.Copy the code

Pay attention to the last sentence, it means: after open the project with CocoaPodsDemo. Xcworkspace open, rather than before. Xcodeproj file.

You may wonder why there are.xcodeProj files. This is the new file you just generated by running the $pod install command. In addition to this file, you’ll find another file “podfile. lock” and a folder “Pods”. Click CocoaPodsDemo. Xcworkspace after open project, Xcode directory structure is as follows:

You will be pleasantly surprised to find that AFNetwoking has successfully imported the project (red box)!

Now you can start using AFNetworking. H. To test this a bit, type in any code file for your project:

#import <AFNetworking. H > or #import "AFNetworking. H"Copy the code

Then compile to see if anything goes wrong. It is impossible to go wrong if you strictly follow the steps described above.

This completes the first application scenario for CocoPods. Although the small preparation so much, in fact, the process is very simple. To sum up:

  1. Start by creating a Podfile in your project. The contents of the Podfile are the libraries you want to import. The author of a generic library will tell you how to write a Podfile when importing the library.
  2. Run the command:`$ pod install.

Next, the editor continues with the second usage scenario.

Scenario 2: How do I correctly compile and run a project that contains the CocoPods class library

You’ve probably been there before (especially new iOS developers), found a piece of code on GitHub that you wanted, downloaded it, compiled it, and found all sorts of bugs. It turns out that various other third-party libraries are missing. Take a closer look and you’ll see that the code you downloaded contains your Podfile. Yes, this means that you can easily download the libraries you need using CocoaPods.

Below, the editor uses the code UAAppReviewManager as an example to show how to properly compile and run a project that contains the CocoPods class library.

UAAppReviewManager is a feature that lets you easily add reminders of ratings to your application. When you go to a lot of UAAppReviewManager address download this code, open the Example engineering (UAAppReviewManagerExample), the compiler, you’ll find Xcode report a lot of mistakes, basic it is said that you compile the code to find so-and-so header files, This means that to compile the Example code for UAAppReviewManager successfully, you must import some third-party libraries first. At the same time, you will find below the UAAppReviewManagerExample folder has three associated with CocosPods file (folder) : Podfile, Podfile. The lock and the Pods, the diagram below:

with

At this time, open a terminal and into the UAAppReviewManagerExample directory, namely and Podfile in the same directory, like scenario 1, enter the following command (because have Podfile, so don’t need to create Podfile) :

$ pod update
Copy the code

After a few seconds (maybe a dozen, depending on your network), the terminal appears:

Analyzing dependencies Fetching podspec for `UAAppReviewManager` from `.. / 'Downloading dependencies Installing UAAppReviewManager (0.1.6) Generating Pods Project Integrating Client project [!]  From now on use `UAAppReviewManagerExample.xcworkspace`.Copy the code

At that time, back to UAAppReviewManagerExample folder to have a look, see more than a document UAAppReviewManagerExample. Xcworkspace:

According to information terminal, you will need new UAAppReviewManagerExample. Xcworkspace to run the Example code.

Open UAAppReviewManagerExample xcworkspace, compile run, success! The diagram below:

If you type $pod update instead of $pod install, you will find that the class library import failed and the terminal will display the following message:

[!]  Required version (UAAppReviewManager (from `.. / ')) not found for 'UAAppReviewManager'. Available versions: 0.1.6Copy the code

The class library has been updated, but the Podfile has not changed. $pod Install will only request the library as requested by Podfile. If the library version number changes, it will fail to retrieve the library. But $POD update will update all libraries to get the latest version of the library. And you’ll find that if you use $pod update, then $pod Install succeeds.

When to use $pod install and when to use $POD Update, you might ask. I don’t know if there is a new version of the library. Well, you can just use $pod update every time. Or use $pod Install first, and if that doesn’t work, use $pod Update.

Well, xiaobian is long winded here. These are just the most basic uses of CocoaPods. To continue exploring other advanced uses of CocoaPods, check out CocoaPods Wiki here.

\