This article has been synchronized to the brief book ios-Cocoapods principles
CocoaPods puts all of its dependency libraries into the Pods project and makes the main project dependent on the Pods project, so the source management work is moved from the main project to the Pods project
- The Pods project will eventually compile into a file called libPods.a, which is all the main project needs to rely on (or possibly a. Framework file)
- For resource files, CocoaPods provides a bash script called Pods-resources.sh that is executed each time it is compiled to copy the various resource files from third-party libraries into the target directory
- CocoaPods uses a file called Pods.xcodeProj to set all dependencies and parameters at compile time
Core components
CocoaPods is written in Ruby and consists of several Ruby packages (gems). The most important gems in parsing integration are: (Yes, CocoaPods is a dependency management tool — built with dependency management!) . CocoaPods is an ObjC dependency management tool, which itself is built using Ruby’s dependency management gems.
- CocoaPods/CocoaPod
This is a user-facing component that is activated every time a POD command is executed. This component includes all of the functionality involved in using CocoaPods, and can also perform tasks by calling all of the other Gems
- CocoaPods/Core
The Core component provides support for processing files associated with CocoaPods, mainly Podfiles and Podspecs
- Podfile
Podfile is a file that defines the third-party libraries that your project needs to use.
- Podspec
Podspec is also a file that describes how a library is added to a project. It supports listing source files, framework, compilation options, and dependencies required by a library.
- CocoaPods/Xcodeproj
This gem component is responsible for the integration of all project files. It can create and modify.xcodeProj and.xcworkspace files. It can also be used as a separate gem package. If you want to write a script that can easily modify a project file, you can use this gem
First we CDS to ~ /. Cocoapods/repos/master/Specs directory, choose one of the third party libraries, view its AFNetwork. Podspec. Json file for AFNetwork (for example) :
{"name": "AFNetwork", "version": "0.1.0", "summary": "Simple Request Manager about AFNetworking", "description": "TODO: Add long description of the pod here.", "homepage": "https://github.com/lingyfh/AFNetwork", "license": { "type": "MIT", "file": "LICENSE" }, "authors": { "lingyfh": "[email protected]" }, "source": { "git": "Https://github.com/lingyfh/AFNetwork.git", "tag" : "0.1.0 from"}, "platforms" : {" ios ":" 8.0 "}, "public_header_files" : [ "AFNetwork/*.h", "AFNetwork/**/*.h" ], "source_files": "AFNetwork/Classes/**/*", "dependencies": { "AFNetworking": ["~> 3.0"]}}Copy the code
As you can see, this contains information for all three parties, including name, protocol, description, Github address, support platform, etc
The pod install process
When we add code to our Podfile to introduce a third-party library, we execute pod Install, and the corresponding third-party library is installed in our Pods project. What happens when pod install is installed?
xxx@xxxdeMacBook-Pro CocoaPodsDemo % pod install --verbose Preparing Analyzing dependencies Inspecting targets to integrate Using `ARCHS` setting to build architectures of target `Pods-defaults-CocoaPodsDemo `: (``) Finding Podfile changes - AFNetworking Resolving dependencies of `Podfile` CDN: trunk Relative path: CocoaPods-version.yml exists! Returning local because checking is only perfomed in repo update Comparing resolved specification to the sandbox Manifest-afnetworking Downloading Dependencies -> Using AFNetworking (3.2.1) - Running pre install hooks Generating Pods project - Creating Pods project - Installing files into Pods project - Adding source files - Adding frameworks - Adding libraries - Adding resources - Linking headers - Installing Pod Targets - Installing target `AFNetworking` iOS 7.0 - Generating dummy source at ` Pods/Target Support Files/AFNetworking AFNetworking - dummy. M ` - Generating deterministic UUIDs - Stabilizing target UUIDs - Running post install hooks - Podfile - Writing Xcode project file to `Pods/Pods.xcodeproj` Cleaning up sandbox directory Integrating client project Integrating target `Pods-defaults-CocoaPodsDemo ` (`CocoaPodsDemo.xcodeproj` project) - Running post integrate hooks - Writing Lockfile in `Podfile.lock` - Writing Manifest in `Pods/Manifest.lock` CDN: trunk Relative path: CocoaPods-version.yml exists! Returning local because checking is only perfomed in repo update -> Pod installation complete! There are 1 dependencies from the Podfile and 1 total pods installed.Copy the code
Analysis process:
- Read the Podfile file to analyze which third-party dependent libraries are available and their versions
- Load source files, each
xxx.podspec.json
Each file contains an index of the source code, which contains a Git address and Git tag that exists as a commit SHAs~/Library/Caches/CocoaPods
Core gem is responsible for creating files in this path, and CocoaPods downloads the source files to the Pods directory according to the Podfile,.PodSpec, and cache files - generate
Pods.xcodeproj
Each time pod Install is pointed to, CocoaPods uses the Xcodeproj gem component pair if a change is detectedPods.xcodeproj
If the file does not exist, the default configuration is used; otherwise, the existing configuration items are loaded into memory - When CocoaPods adds a third party library to your project, you add a lot of content as well as code. Because each third party library has a different Target, there are several files that need to be added to each library, and each Target needs to:
- A.xcconfig file with compilation options
- A private.xcconfig file that contains both the compile Settings and the CocoaPods default configuration
- A prefix-.pch file required for compilation
- Another file required for compilation is dummy.m
Take AFNetworking as an example:
Once the target for each pod is complete, the entire pod target is created. This adds the same file along with several other files. If the source code contains the resource bundle, The instruction to add the bundle to target will be added to the pod-resources.sh file. There is also a file called Pods-environment.h, which contains macros that can be used to check whether a component is from a POD, resulting in two approved files, a plist and a Markdown, which are used to refer to license information for the end user
- Writing to disk, until now, much of the work was done in memory. In order for these results to be reused, we need to keep all the results in one folder, so the Pods.xcodeProj file is written to disk, and two other very important files, Podfile.lock and manifest.lock, are written to disk
- Podfile.lock, one of the most important files created by CocoaPods, records each installed version of a POD that needs to be installed. If you want to know which version of pod you have installed, you can look at this file. It is recommended that you add the podfile. lock file to your version control to help with consistency across your team
- Mainfest.loc This is a copy of the podfile. lock file that is created each time you run the pod install command. If you’ve ever encountered an error such as the sandbox file being out of sync with the podfile. lock file (
The sandbox is not in sync with the Podfile.lock
), this is caused by a discrepancy between the manifest. lock file and the podfile. lock file. Since the directory where Pods are located is not always under version control, this ensures that developers can update their Pods before launching the app, otherwise the app may crash or fail to compile somewhere - Xcproj, if you have installed xcPROj on your system as recommended, it will perform a touch on the Pods.xcodeProj file to convert it to the old ASCII PList file. Why do you do that? Although long gone from being supported by other software, Xcode still relies on this format. Without xcPROj, your Pods.xcodeProj file will be stored as a PList file in XML format, and when you open it with Xcode, it will be overwritten and cause a lot of file changes
Reference: What does CocoaPods do? Cocoapods series tutorials – Principles