- directory
- preface
- CocoaPods installation
- CocoaPods principle
- CocoaPods use
1, the preface
CocoaPods is a third class library management tool for OS X and iOS. It allows you to add dependent libraries called “Pods” to your project (these libraries must be supported by CocoaPods themselves) and make it easy to manage their versions.
2. Install CocoaPods
1. Set up the Ruby source
The Mac OS comes with a Ruby environment installed
1.1. Check the Ruby source on your computer
- ruby -v
ruby -v
Copy the code
- If your computer version is lower than this and you want to upgrade Ruby, it shows that my computer version does not need to upgrade, you can ignore the following upgrade operation
sudo gem update --system
Copy the code
- If you don’t have a Ruby environment, execute the following command
gem install ruby
Copy the code
1.2. Replace the Ruby image
- Ruby’s default source address is a foreign network address. Run the following command to view the current image
gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
Copy the code
- Remove the current Mirror
gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
Copy the code
- Add domestic Ruby images
gem sources -a https://gems.ruby-china.com/
https://gems.ruby-china.com/ added to sources
Copy the code
- The mirror is replaced successfully
gem sources -l
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
Copy the code
2. Install CocoaPods
Once your Ruby environment is installed, you can install CocoaPods
2.1 Checking the POD Version
pod --version
Copy the code
2.2 uninstall CocoaPods
Sudo gem uninstall cocoapods -v 1.9.3Copy the code
2.3 installation
Methods a
Sudo gem install cocoapods -v 1.9.3 sudo gem install cocoapodsCopy the code
After a few minutes, the following information is displayed to indicate that the installation is successful.
Method 2
sudo gem install -n /usr/local/bin cocoapods
Copy the code
2.2 Verifying the installation
- You can check the POD version command to see if the installation is successful
pod --version
Copy the code
2.3 Downloading Files
pod setup
Copy the code
2.4 Verifying the installation
pod search AFNetworking
Copy the code
3. CocoaPods principle
Core components
CocoaPods is an ObjC dependency management tool, which itself is built using Ruby’s dependency management gems.
CocoaPods is written in Ruby and consists of several Ruby packages (gems). The most important gems in parsing integration are:
- CocoaPods/Specs
- CocoaPods/CocoaPods
- CocoaPods/Core
- CocoaPods/Xcodeproj
CocoaPods/Specs
This is a repository that holds podSpec files for third-party components. Once third-party components are developed, a PodSpec file is passed to CocoaPods. This Specs contains all the versions of the PodSpec file for each third-party component. When using a third-party component that supports CocoaPods, you specify the source in your Podfile, as follows:
source 'https://github.com/CocoaPods/Specs.git'
Copy the code
When commands such as Pod Install or POD Update are executed, the podSpec file for the specified version of the component is retrieved from the repository and the component is retrieved based on this PodSpec configuration information.
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 Core
The component provides support for processing files associated with CocoaPods, mainly Podfiles and Podspecs.
When executing commands such as pod Install. The Core component parses podSpec files uploaded by third-party component developers and consumer Podfiles to determine which files need to be imported into the project. In addition, this component handles commands related to these files, such as using pod Spec Lint to check the validity of podSpec files.
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.
Podfile
Podfile is a file that defines the third-party libraries that your project needs to use. The file is highly customizable and you can customize it to your personal preference.
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.
Ruby overview
methods
- The easy way
Ruby code can omit parentheses when calling methods.
def method_name (var1=value1, var2=value2)
expr..
end
Copy the code
Methods in Ruby start with ‘def’ and end with ‘end’. We can set default values for arguments, and use the default values if the method is called without passing the required arguments, such as value1 and value2 in the above example.
- Variable number of arguments
Def sample (*test) puts "#{test.length}" for I in 0... The test.length puts "parameter value is #{test[I]}" end end sample "Zara", "6", "F".Copy the code
Data type –Hash
A Hash is a set of key-value pairs like “key” => “value”. A Hash is similar to an array, except that its index (or “key”) is not limited to numbers. The index of a Hash can be almost any object.
Hash is similar to arrays, but with one important difference: the elements of a Hash are in no particular order. If order is important, use arrays.
pod 'SwViewCapture', :git=>'[email protected]:startry/SwViewCapture.git', :branch=>'master'
Copy the code
def pod(name = nil, *requirements)
unless name
raise StandardError, 'A dependency requires a name.'
end
current_target_definition.store_pod(name, *requirements)
end
Copy the code
The argument that follows the pod method is a Hash object, written as a key-value
{
":git": "[email protected]:startry/SwViewCapture.git",
":branch": "'master'"
}
Copy the code
block
The part between do and end in Ruby is called a block, which can also be written as {.. } So the target method can be called in this form:
Target (' PodSample ') {pod (' SDWebImage ', '~ > 4.4.2')}Copy the code
It can also be called using our usual form:
Target ('PodSample') do pod('SDWebImage', '~> 4.4.2') endCopy the code
eval
This method executes strings as code, which means eval blurs the boundary between code and data. Eval is also used to execute JS code in iOS development.
eval "1 + 2 * 3" => 7
Copy the code
Podfile&Podfile. Lock the parsing
Podfile
CocoaPods internally defines configuration methods that convert method parameters into properties of internal Hash variables.
Podfile.lock
The podfile. lock file is written in the data description language YAML(YAML Ain’t Markup). YAML(YAML Ain’t Markup) is a compact, data-centric, non-markup language that uses whitespace, indentation, and lines to organize data, making presentation concise and readable. Internally, CocoaPods hashes podfile. lock files for querying parameter data and comparing them to the parameters in Podfile files.
4. Use CocoaPods
- Remote Pods Library:
- This can be a CocoaPods public library or a custom Git repository (remote private library)
- Local index library:
- The location can be accessed using the command $CD ~/.cocoapods/repos. Both public and private libraries will be in this path
- Source code library:
- These can be libraries in the CocoaPods public Git repository, or they can be replaced by any Git, Mercurial, or SVN repository, and you can specify specific commit, branch, or tag.
basis
1. Create a Podfile
Create a Podfile (if you have a Podfile, do not execute pod init)Copy the code
Note: The Podfile details the targets dependencies for one or more projects
2. Add third-party library dependencies to Podfile
Target 'QSAppDemo' do pod 'AFNetworking' pod 'YYModel', '~> 1.0.4' endCopy the code
3. Download and install the third-party library
pod install
Copy the code
- Once the download is successful, use the.xcworkspace file generated by CocoaPods to open the project. Each time you change the Podfile, re-execute the pod update command.
- If pod install or POD update depends on dependencies, add –verbose –no-repo-update to CocoaPods spec repository and skip this step. If pod install or POD update depends on Dependencies, add –verbose –no-repo-update to CocoaPods Spec repository.
Pod specifies the dependency version range
- If the version is not specified after the dependency, the latest version is used by default
pod 'AFNetworking'
Copy the code
Version control
> 0.1 Any version later than 0.1 (excluding 0.1) >= 0.1 Any version later than 0.1 (including 0.1) < 0.1 Any version earlier than 0.1 (excluding 0.1) <= 0.1 Any version earlier than 0.1 (including 0.1) ~> 0.1.2 version 0.1.2 to 0.2, excluding 0.2. This is based on the last part of the version number you specified. This example is equivalent to >= 0.1.2 and <0.2.0, and is always the latest version in your specified range.Copy the code
Pods specify branches or nodes that depend on libraries
- Importing the Master branch (default)
pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git'
Copy the code
- Imports the specified branch
pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git', :branch => 'develop'
Copy the code
- The code that introduces a node
Pod 'AFNetworking' : git = > 'https://github.com/AFNetworking/AFNetworking.git' : tag = > '2.7.0'Copy the code
- Introduce a particular commit node
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => '685e31a31bb1ebce3fdb5a752e392dfd8263e169'
Copy the code
- Using local files
We can also specify the source address of the dependent library. If we wanted to import one of our local libraries, we could write:
pod 'AFNetworking', :path => '~/Documents/AFNetworking'
Copy the code
Some configuration instructions for Podfile
- Podfile explains how to use it
Source:
Specify the source of the POD. If you do not specify source, CocoaPods official source is used by default
Official # using the default address (the default) source 'https://github.com/CocoaPods/Specs.git # company private library source' private library url 'Copy the code
Target Configuration
Platform Specifies the platform on which static libraries should be built. CocoaPods provides a default platform version configuration:
Platform :ios, '4.0' platform :iosCopy the code
use_frameworks! :
Using this command will generate the Frameworks that depend on the libraries in the Products directory of the Pods project; otherwise, a static library of.a will be generated in the Products directory of the Pods project.
target 'Demo' do
use_frameworks!
end
Copy the code
Dependencies
Podfile specifies the dependencies for each target
Pod specifies a specific dependency library
Podspec provides an API to create PodSpecs
Target Specifies a dependency range by target
target
Define the pod target (the target in the Xcode project) and specify the scope of dependencies within the given block. A target should be associated with the Target of an Xcode project. By default, target contains dependencies defined outside the block, unless you specify that inherit is not used! To inherit (referring to inheritance in nested blocks)
Defining Apptarget only introduces the AFNetworking library. Defining AppTeststarget with Nimble also inherits the AFNetworking library from Apptarget
target 'App' do
pod 'AFNetworking'
target 'AppTests' do
inherit! :search_paths
pod 'Nimble'
end
end
Copy the code
- A target block contains multiple nested subblocks
Target 'ShowsApp' do # ShowsApp only introduces ShowsKit pod 'ShowsKit' # ShowTVAuth Target 'ShowsTV' do pod 'ShowTVAuth' end # introduces Specta and Expecta as well as ShowsKit target 'ShowsTests' do inherit! :search_paths pod 'Specta' pod 'Expecta' end endCopy the code
- Abstract target
Define a new abstract target that can be easily used for target dependency inheritance.
Simple notation
abstract_target 'Networking' do
pod 'AlamoFire'
target 'Networking App 1'
target 'Networking App 2'
end
Copy the code
Define an Abstract_target that contains multiple targets
# note: ShowsKit abstract_target 'Shows' do pod 'ShowsKit' # ShowsiOS Target 'ShowsiOS' do pod 'ShowWebAuth' end # ShowsTV Target 'ShowTVAuth' do pod 'ShowTVAuth' end # ShowsTests Target introduces the Specta and Expecta libraries ShowsKit target 'ShowsTests' do inherit! :search_paths pod 'Specta' pod 'Expecta' end endCopy the code
Build Configurations
By default, dependencies are installed in build Configuration for all targets. Dependencies can only be enabled in a given Build Configuration for debugging or other reasons.
The following notation indicates that configuration is enabled only in Debug and Beta mode
pod 'MJRefresh', :configurations => ['Debug', 'Beta']
Copy the code
Alternatively, you can whitelist only one build configurations.
pod 'MJRefresh', :configuration => 'Debug'
Copy the code
Note: by default, if you do not specify the specific build configuration, it will be included in all configurations. If you want to specify it, you must specify it manually.
Subspecs
Normally we’ll use the name of the dependency library to import it, and cocoapods will install all the content of the dependency library by default.
We can also specify to install a submodule of a specific dependent library, for example:
Pod 'QueryKit/Attribute'Copy the code
Pod 'QueryKit', :subspecs => ['Attribute', 'QuerySet']Copy the code
def
We can also declare a pod set with the def command:
def 'CustomPods'
pod 'IQKeyboardManagerSwift'
end
Copy the code
We can then introduce at the target we want to introduce:
target 'MyTarget' do
CustomPods
end
Copy the code
The nice thing about this is that if you have multiple targets and not all of them are included, you can separate them in this way.
post_install
When our installation is complete, but the generated project has not been written to disk, we can specify the action to be performed.
For example, we can modify the configuration of some projects before writing to disk:
post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['GCC_ENABLE_OBJC_GC'] = 'supported' end end endCopy the code
pre_install
When the download is complete but not yet installed, you can use the hook mechanism to specify changes via pre_install and then proceed to the installation phase.
Pre_install do | installer | # to make some changes to the end before installationCopy the code
abstract! And inherit!
-
abstract! Indicates that the current target is abstract and therefore does not link directly to the Xcode target.
-
inherit! Sets the inheritance mode for the current target. Such as:
target 'App' do
target 'AppTests' do
inherit! :search_paths
end
end
Copy the code
inhibit_all_warnings!
inhibit_all_warnings! Mask all warnings from cocoapods dependent libraries. You can define it globally, within a sub-target, or specify a library:
# Hide SSZipArchive warnings without hiding ShowTVAuth warnings Pod 'SSZipArchive', :inhibit_warnings => true pod 'ShowTVAuth', :inhibit_warnings => falseCopy the code
CocoaPods creates private libraries
- Resources: CocoaPods creates a private library
CocoaPods pulls open source libraries
CocoaPods has an open source index repository for Specs, which houses the. Podspec files for all versions of the open source libraries. The. The first time you use CocoaPods, you clone the file library locally to ~/. CocoaPods /repos/master.
- Executing pod Install in the Podfile directory will look for the local index library, and podsepc will pull the latest index library from the remote if it does not exist locally.
- Get the source address from the podspec file found in the index library.
- Pull the corresponding version of the code from the source address.
As you can see, importing an open source library is slow the first time and fast the next time. This is because CocoaPods has a local cache directory where the source code of the open source library is stored. After the first download, when the library is imported again, it is copied directly from the local directory.
Check the cache list using pod cache list, cache path for ~ / Library/Caches/CocoaPods/Pods.
Steps to create a private library:
1. Create a PodSpec file
1.1 What is podSpec? Folder open ~/.cocoapods/repos see
Specs are stored in cocoapod’s specification: specification library. Check the podSpec file for the resource URL and clone it into our pod. Read as a catalog of books, we’re actually looking for this specs collection when we look for third-party libraries. So the first step to creating a private library is to create our spec directory.
Create an empty repository, preferably with a spec suffix to distinguish the repository from the specification
Then add the library to the ~ /. Cocoapods/repos, the operation is very simple, terminal pod command pod repo add hxkSpec [email protected]: xiaoyiqiu/spec. Git
1.2 Start creating the pod private library PodSpec
Create a specification repository on Github, not the source code repository
I recommend creating an empty repository, preferably with a spec suffix to distinguish the repository from the specification
Then add the library to the ~ /. Cocoapods/repos, the operation is very simple, terminal pod command pod repo add TDNIMKitSpec [email protected]: strivever/TDNIMKitSpec git
Command format:
Pod repo add [specName]Copy the code
2. Make our POD library
2.1 create the library
Create your pod libraryCopy the code
2.2 Configuration.Podspec file information
Podspec file syntax
Pod: : Spec. New do # | s | Pod private library name s.n ame = 'TDNIMKit' # Pod private library version number s.v ersion = '0.1.0 from' # Pod private library profile s.s ummary = 'A short description of TDNIMKit.' # This description is used to generate tags and improve search results. # * Think: What does it do? Why did you write it? What is the focus? # * Try to keep it short, snappy and to the point. # * Write the description between the DESC delimiters below. # * Finally, don't worry about the indent, CocoaPods strips it! s.description = <<-DESC TODO: Add long description of the pod here. DESC # main, It is best to visit Sheldon horowitz omepage = 'https://github.com/[email protected]/TDNIMKit' # s.s creenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' # open source licenses s.l icense = {: type = > 'MIT, :file => 'LICENSE'} # open source author. Clone when they need to use this address to version pull s.s ource = {: git = > 'https://github.com/[email protected]/TDNIMKit.git', :tag =>'0.1.0'} # Supported system version s.iso. Deployment_target = '8.0' # source relative path s.source_files = 'TDNIMKit/Classes/**/*' # s.resource_bundles = { # 'TDNIMKit' => ['TDNIMKit/Assets/*.png'] # } # s.public_header_files = 'Pod/Classes/**/*.h' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Copy the code
- CD to the source folder:
pod lib lint [email protected]:xiaoyiqiu/spec.git,https://github.com/CocoaPods/Specs.git --allow-warnings
Copy the code
pod repo push spec huang.podspec [email protected]:xiaoyiqiu/spec.git,https://github.com/CocoaPods/Specs.git --allow-warnings
Copy the code
2.3 Verify that the library has no problem
Verify that the PodSpec file is correct locally
CD to the XXX. Podspec directory: Execute the following command
pod lib lint --use-libraries --allow-warnings
Copy the code
2.4 Writing Code
2.5 Upload to gitLab hosting platform
- Upload to the remote server
- Make sure you put a tag on it. It must match the tag specified in the PodSpec file
- Make sure that the remote repository resource is synchronized, or else the resource will not match later.
2.6 Remote Warehouse Verification
- Method 1 (recommended):
Pod lib lint - sources = 】 【 gitLab SSH address, https://github.com/CocoaPods/Specs.git, allow - warnings pod lib lint - [email protected]: xiaoyiqiu/spec. Git (the address of the remote repository), https://github.com/CocoaPods/Specs.git, allow - warningsCopy the code
- Method 2:
pod spec lint --use-libraries --allow-warnings
Copy the code
Validation can be lengthy, so you can add –verbose to view logs
2.6 Publishing private libraries
Execute the following issuing instructions:
- Method one:
// pod command: Pod repo push spec huang. Podspec [local spec folder name] podspec file --sources= gitLab SSH address 】, https://github.com/CocoaPods/Specs.git - allow - warnings pod repo push spec. Huang podspec [email protected]:xiaoyiqiu/spec.git,https://github.com/CocoaPods/Specs.git --allow-warningsCopy the code
- Method 2:
Pod command pod repo push [local spec folder name] [Podspec file to publish] --use-libraries --allow-warnings --verboseCopy the code
- After successful publishing, check that both local and remote podSpecs are available
2.7 Verify that the private warehouse is available
- $pod search [private library name]
- If you can’t find it, The terminal execute the following command rm/Library/Caches/CocoaPods/searchindex json or to update the local rm warehouse ~ / Library/Caches/CocoaPods/search_index json or to update the local warehouse rm/Library/Caches/CocoaPods/searchindex json or update the local repository pod repo Update and retry the search
2.8 Using private libraries
6, commonly used POD instruction
Pod setup principle
The essence is to github.com/CocoaPods/S… /Users/ username /.cocoapods/repos. If the project already exists in this directory, use the pod setup command to update the project to its latest state.
Differences between Pod Instal and Pod Update
-
When executing pod install, if the podfile. lock file exists, download the version of the installation specified in the podfile. lock file. For the POD libraries that are not in the podfile. lock file, The pod install command searches for the version of the pod library specified in the Podfile to install.
-
Use pod Update only when you want to update the version of the POD library; It reads the framework information of the Podfile to download and install it, regardless of whether or not it exists. Once the file is downloaded, it generates the podfile. lock file based on the framework information
// Generate a Podfile and write the required third-party file $pod initCopy the code
// Install the third party library. If the third party library is installed successfully, the file 'podfile.lock' is generated to record the third party library version. $pod install $pod install --no--repo--install --no--repo--install --no--repo--installCopy the code
$pod update $pod update --no--repo--update $pod update --no--repo--updateCopy the code
$pod Search Specifies the name of the third party libraryCopy the code
$pod --versionCopy the code
$pod repo update $pod repo updateCopy the code
// Use pod to view the added repo $pod repo listCopy the code
Clear related commands
Pod cache clean -all # delete pod cache rm ~/Library/Caches/Cocoapods/Pods/Pods/ReleaseCopy the code
other
# remove search_index. Json file rm ~ / Library/Caches/CocoaPods/search_index jsonCopy the code
reference
-
CocoaPods website
-
CocoaPods principle
-
CocoaPods source
-
If there is infringement, contact must delete!
-
If there are incorrect places, welcome to guide!
-
If you have any questions, feel free to discuss them in the comments section!