[Cocoapods] installation and use of the third-party library management tool Cocoapods

Reprint address: https://www.jianshu.com/p/9727434520b0

brief introduction

CocoaPods is a tool responsible for managing the third-party open source code in iOS projects. Using CocoaPods can save time and improve work efficiency in setting up and updating the third-party open source library.
How to install Cocoapods:

1, Build ruby environment

In general, Mac OS has its own Ruby environment. You can enter ruby-v in the terminal to view the current version

macdeMacBook-Pro:~ Artron_LQQ$ ruby -v  
ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]  
macdeMacBook-Pro:~ Artron_LQQ$ 

If an update is required, use the following amount command:

sudo gem update --system 

Then replace Ruby image with taobao image to avoid being blocked by walls

Terminal input:
gem sources --remove https://rubygems.org/
gem sources -a http://ruby.taobao.org/

As follows:

macdeMacBook-Pro:~ Artron_LQQ$ gem sources --remove  https://rubygems.org/   
https://rubygems.org/ removed from sources  
macdeMacBook-Pro:~ Artron_LQQ$ gem sources -a http://ruby.taobao.org/   
http://ruby.taobao.org/ added to sources  

Enter the command gem sources -l to view the current Ruby image:

macdeMacBook-Pro:~ Artron_LQQ$ gem sources -l  
*** CURRENT SOURCES ***  
  
https://ruby.taobao.org/  
macdeMacBook-Pro:~ Artron_LQQ$   

The Ruby image is replaced successfully. Next, install cocoapods!

PS: if the replacement is not successful, it can be changed to: gem sources -a https://ruby.taobao.org/

Note: some people don't use taobao's Ruby, but Ruby China , the use of which is roughly the same. Note:
Please try to use a newer version of RubyGems, which is more than 2.6.x recommended.

$ sudo gem update --system # Please go over the wall here
$ gem -v
2.6.3

Then, replace:

$ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
$ gem sources -l
https://gems.ruby-china.org
# Make sure that only gems.ruby-china.org

PS: if the above command is used, an error similar to the following is reported:

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the ...

This is a permission problem. You can add * * sudo * * before the command, so you need to enter the computer password. Note that when you enter the password here, there will be no display. Just enter the end and enter
For example, the above error was encountered when upgrading gem. At the beginning, my instructions are:

gem update --system

In this way, the above error will be reported. Change it to the following:

sudo gem update --system

2, Install Cocoapods

Open the terminal and enter the following command:

sudo gem install cocoapods 

Wait for the installation to complete!
Then enter the pod setup command to configure cocoapods (you can also leave it blank. If you don't enter it, it will be automatically executed the first time you use the pod command)

If you need to view the current version of Cocoapods, use the following instructions:

pod --version

If you need to upgrade Cocoapods, you can enter the above installation instructions, or:

sudo gem install cocoapods --pre

If your Mac OS system is at 10.11 +, the following error will be reported:

ERROR:  While executing gem ... (Errno::EPERM)
    Operation not permitted - /usr/bin/xcodeproj

perhaps

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/bin directory.

Because there is no permission to write, you only need to change the instruction to the following one and change the save path:

sudo gem install -n /usr/local/bin cocoapods --pre 

3, Easy to use

1. Enter the root directory of the project

Terminal input cd project root directory
PS: some partners don't know how to view the directory of files, but it's very simple. Enter cd and leave it blank, then drag the folder to the terminal to automatically generate the directory of files!

macdeMacBook-Pro:~ Artron_LQQ$ cd /Users/mac/Desktop/LQQAFNetworking2.0  
macdeMacBook-Pro:LQQAFNetworking2.0 Artron_LQQ$  

The following 2-4 steps are not recommended now. Create a Podfile. After cd to the root directory of the project, directly enter pod init to create a new Podfile with the corresponding target. Double click to open it. Add the desired third-party library header file in it!

2. Create a new Podfile file

Terminal input touch Podfile (note the case of Podfile):

macdeMacBook-Pro:LQQAFNetworking2.0 Artron_LQQ$ touch Podfile  
macdeMacBook-Pro:LQQAFNetworking2.0 Artron_LQQ$  

At this time, there will be multiple podfiles in the project root directory

3. Open the Podfile file

Enter vi Podfile in the terminal to open the file:

~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
"Podfile" 0L, 0C 

4. Edit the Podfile file

The terminal input i to enter the file editing mode, and then input the library file to be installed

platform :iOS,'7.0'  
pod "AFNetworking","~>2.5"  
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
~                                                                                 
-- INSERT --  

platform: indicates the minimum system version supported by the class library
The pod command is used to import a third-party library. The "~ > 2.5" after it is the version of the specified import library. If it is not added, the latest version will be imported

After editing, press esc key to enter command line mode, then press shift +: and input WQ (save and exit). Press enter to save the editing content and exit editing!
If you do not know whether the three-party library supports pod or the version of the library, you can use the pod search command to query:

macdeMacBook-Pro:LQQAFNetworking2.0 Artron_LQQ$ pod search AFNetworking  
  
  
-> AFNetworking (3.0.4)  
   A delightful iOS and OS X networking framework.  
   pod 'AFNetworking', '~> 3.0.4'  
   - Homepage: https://github.com/AFNetworking/AFNetworking  
   - Source:   https://github.com/AFNetworking/AFNetworking.git  
   - Versions: 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-beta.3, 3.0.0-beta.2,  
   3.0.0-beta.1, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.4, 2.5.3, 2.5.2, 2.5.1, 2.5.0,  
   2.4.1, 2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0, 2.1.0, 2.0.3,  
   2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 1.3.4, 1.3.3, 1.3.2,  
   1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0, 1.0RC3, 1.0RC2, 1.0RC1,  
   0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master repo]  
   - Subspecs:  
     - AFNetworking/Serialization (3.0.4)  
     - AFNetworking/Security (3.0.4)  
     - AFNetworking/Reachability (3.0.4)  
     - AFNetworking/NSURLSession (3.0.4)  
     - AFNetworking/UIKit (3.0.4)  
macdeMacBook-Pro:LQQAFNetworking2.0 Artron_LQQ$   

5. Download files

After the completion of the above, input: pod install in the terminal, then download the required library file, which may take a long time. After the download is successful, the output will be as follows:

macdeMacBook-Pro:LQQAFNetworking2.0 Artron_LQQ$ pod install  
Updating local specs repositories  
  
CocoaPods 1.0.0.beta.2 is available.  
To update use: `gem install cocoapods --pre`  
[!] This is a test version we'd love you to try.  
  
For more information see http://blog.cocoapods.org  
and the CHANGELOG for this version http://git.io/BaH8pQ.  
  
Analyzing dependencies  
Downloading dependencies  
Installing AFNetworking (2.6.3)  
Generating Pods project  
Integrating client project  
  
[!] Please close any current Xcode sessions and use `LQQAFNetworking2.0.xcworkspace` for this project from now on.  
Sending stats  
Pod installation complete! There is 1 dependency from the Podfile and 1 total  
pod installed.  
macdeMacBook-Pro:LQQAFNetworking2.0 Artron_LQQ$   

So far, the installation and download of Cocoapods are finished!
When you open the project folder, you will find that there are multiple Pods folders, Podfile, Podfile.lock, and a. xcworkspace file. When you open the project again, you cannot use. xcodeproj, but the newly generated. xcworkspace file
pod not only configures the three-party library files for us, but also configures the configuration in the project that we need to use these library files. When using, we only need to import the corresponding header files directly!

PS: if the header file cannot be found, you can set it as follows:
Search Search Paths in Build Settings of the project, then add $(SRCROOT) in User header search paths and select recursive

When installing for the first time, you need to use terminal instructions to operate the whole process. After adding a pod to the project, you can open the project, edit it directly in the Podfile in the project, and add the corresponding library

If you need to add a new library, add it in the Podfile file: pod third-party library name
If you need to delete, delete the library file from the Podfile file

Then, input pod install in the terminal and wait for it to finish!

Note: the precondition for operating pod instruction in the terminal is to cd to the root directory of the project

PS: if you use pod install or pod update in the future, you may be stuck in the Updating local specs repositories for a long time. This is to upgrade the local specs repository of cocoa pods, which is not necessary in most cases, so you can use the following instructions to skip the upgrade:

pod install --verbose --no-repo-update  
pod update --verbose --no-repo-update  
//perhaps  
pod install --no-repo-update  
pod update --no-repo-update  

(end)


 

Published 158 original articles, won praise 2, visited 10000+
Private letter follow

Keywords: Ruby sudo iOS Mac

Added by Parody on Thu, 16 Jan 2020 10:14:59 +0200