Cocoapods is a tool for managing third-party open source code in iOS projects. Using Cocoapods can save us the time to set up and update third-party open source libraries. The following two sections describe how to install and use Cocoapods.
First, install the Cocoapods
First use the following command to upgrade the Ruby version:
?
After waiting for the update to complete, then download the installation Cocoapods by using the Gem command:
?
After the installation is complete, call the following command to set the Cocoapods:
?
After executing the above command, the installation is successful! The following describes how to use Cocoapods.
Second, the use of cocoapods
Open the terminal and use the CD command to navigate to the project root path where you want to use Cocoapods. Such as:
?
1 |
cd /Users/username/Documents/Projects/Demo |
Consider which libraries you need to reference in your project, and then, using the Search command to find information about the library, if you need to add Jsonkit, you can do the following:
?
When the command executes, it gets the following result:
?
123456 |
-> JSONKit (1.5pre)
A Very High Performance Objective-C JSON Library.
pod
‘JSONKit‘
,
‘~> 1.5pre‘
- Homepage: https:
//github
.com
/johnezang/JSONKit
- Source: https:
//github
.com
/johnezang/JSONKit
.git
- Versions: 1.5pre, 1.4 [master repo]
|
One of the pod ' Jsonkit ', ' ~> 1.5pre ' is the information that will be written to Podfile later. If you already know this information, you can omit this step directly. Proceed directly to the following steps.
Once you have the necessary library information, you now need to build the Podfile file in your project. Use the following command:
?
Then edit the Podfile file with the following command:
?
Enter the following in the popup editing screen:
?
123 |
platform :ios pod ‘JSONKit‘ , ‘~> 1.5pre‘ |
Then invoke the following command to initialize:
?
After running, the original project directory will have some extra files.
More *.xcworkspace pods and other files. This is the project management file that pod generates, open the Libdemo.xcworkspace project file, and see in Xcode the directory structure:
One thing to note here is that if you do not call the Create Podfile command, you call the pod Install command directly. You will be prompted "
[!] No ' podfile ' found in the current working directory. "
If you added a new library to Podfile, you can use the following command to update it:
?
Now that all of your third-party libraries have been downloaded and set up with compilation parameters and dependencies, you just have to remember the following 2 points:
1. Use the. xcworkspace file generated by Cocoapods to open the project instead of the previous. xcodeproj file.
2, each time you change the Podfile file, you need to re-execute the pod install or pod Update command.
Cocoapods Installation and use