I do not know the true face of lushan mountain, but I am in it
The original link
An overview,
1. What is CocoaPods?
-
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.
-
IOS program dependency management tool Carthage(Carthage is written by Swift language, only support dynamic framework, only support iOS8+), Carthage and CocoaPods can be used together in the project, but it is recommended to use one, PERSONALLY recommend CocoaPods.
-
CocoaPods project source
2. Download and install CocoaPods
-
CocoaPods requires a Ruby environment. Install Ruby and modify the Ruby image as follows:
# install ruby gem install ruby # upgrade ruby sudo gem update --system # check version ruby -v Update the Ruby image gem sources --remove https://rubygems.org/ gem sources -a https://ruby.taobao.org/ # Check whether the image is updated successfully gem sources -l Copy the code
-
Download and uninstall commands
# Download the latest version sudo gem install cocoapods (sudo gem install -n /usr/local/bin cocoapods) # Download the specified version sudo gem install cocoapods -v 1.6.1 # uninstall sudo gem uninstall cocoapods # Uninstall the specified version sudo gem uninstall cocoapods -v 1.6.1 Check the POD version pod --version Copy the code
-
You can also use Gemfile in the project directory to specify the version to use Cocoapods.
gem "cocoapods".'~ > 1.6.1' Copy the code
3. CocoaPods app
- Introduce excellent third-party libraries for the project, such as SDWebImage, AFNetworking and YYCache
- Create private Pods, split and sink project neutron modules (for internal use)
- Create public Pods and share good features
Podfile basic use
1, the basic
-
Create the Podfile file
Create a Podfile in the root directory of your project. pod init Copy the code
Note: The Podfile details the targets dependencies for one or more projects
-
Podfile adds third-party library dependencies
target 'QSAppDemo' do pod 'AFNetworking' pod 'YYModel'.'- >' 1.0.4 pod 'OOMDetector'.'1.3' The command takes effect in Debug mode pod 'FLEX'.'~ > 2.0'.:configurations= > ['Debug'] pod 'WebViewJavascriptBridge'.:git= >'https://github.com/marcuswestin/WebViewJavascriptBridge.git' end Copy the code
-
Download and install third-party libraries
pod install Copy the code
Note 1: After downloading the project successfully, use the.xcworkspace file generated by CocoaPods to open the project. Each time you change the Podfile, re-execute the pod update command.
2: 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.
2. Pod specifies the dependency version range
-
If the version is not specified after the dependency, the latest version is used by default
pod 'OOMDetector' Copy the code
-
If a dependency is followed by a specific version, the specified version is used
pod 'OOMDetector' Copy the code
-
> 0.1 Any version later than 0.1 (excluding 0.1)
-
>= 0.1 Any version later than or including 0.1
-
< 0.1 Any version less than 0.1 (excluding 0.1)
-
<= 0.1 any version less than or including version 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.
3. Pod specifies branches or nodes that depend on the library
-
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
4, Some configuration instructions for Podfile
-
Source: Specifies the Source of the POD. If you do not specify source, CocoaPods official source is used by default
# use the official default address (default) source 'https://github.com/CocoaPods/Specs.git' Copy 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 will be generated in the Products directory of the Pods project.
target 'QSAppDemo' do use_frameworks! end Copy the code
For details on the Syntax of Pofile, see Podfile Syntax Reference or Podfile Syntax
Cocoapods create a private Pod library
1, an overview of the
-
Due to business needs, we need to develop a business SDK to facilitate business access within the company. We choose to create a private Pod library.
-
In essence, creating a public Pod library is the same as creating a private Pod library. The only difference is that the version index query is different. The public Pod library is managed by CocoaPods/Specs, while the private Pod library requires the company to set up an internal repository to manage podSpecs.
-
A private Spec repO can be built either on a private Git server or on a native git server. Typically, a private spec repO is created on top of an in-house Git server
Add remote private repository to local pod repo add qs_private_pods_specs https://github.com/buaa0300/qs_private_pods_specs.git After adding qs_private_pods_specs, open ~/.cocoapods/repos to see the added qs_private_pods_specs Copy the code
2. Create a private Pod library
pod lib create QSBizSDK # QSBizSDK is the name of the SDK
What platform do you want to use?? [ iOS / macOS ]
> iOS
What language do you want to use?? [ Swift / ObjC ]
> ObjC
Would you like to include a demo application with your library? [ Yes / No ]
> YES
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> None
Would you like to do view based testing? [ Yes / No ]
> No
What is your class prefix?
> QS
......
Copy the code
QSBizSDK downloads a pod template and customizes it internally by modifying the configuration of the podspec file, which describes the instructions for the pod. 2: Pod lib create ProjectName is the default parameter –template-url=https://github.com/CocoaPods/pod-template.git
Note 3: When you host your project in Git, private libraries are typically uploaded to an internal repository.
3. Podspec editing
Pod::Spec.new do |s|
s.name = 'QSBizSDK'
s.version = '1.0.0'
s.summary = 'A short description of QSBizSDK.'
s.description = <<-DESC
Add long description of the pod here.
DESC
s.homepage = 'https://github.com/buaa0300/QSBizSDK'
s.license = { :type= >'MIT'.:file= >'LICENSE' }
s.author = {'the south China coder'= >'[email protected]'}
s.source = { :git= >'https://github.com/buaa0300/QSBizSDK.git'.:tag => s.version.to_s }
s.ios.deployment_target = '8.0'
s.default_subspec = 'QSCore'
# Each sub-module of the individual source path
s.subspec 'QSCore' do |qscore|
qscore.source_files = 'Classes/QSCore/**/*'
end
# A A must be different, is an alias, add good exposed header file
s.subspec 'A' do |a|
a.source_files = 'Classes/QSCore/A/**/*'
a.public_header_files='Classes/QSCore/A/*.h'
end
You can add internal own modules, or external dependencies
s.subspec 'B' do |b|
a.source_files = 'Classes/QSCore/B/**/*'
a.public_header_files='Classes/QSCore/B/*.h'
b.dependency 'Classes/QSCore/A'
b.dependency 'AFNetworking'.'~ > 3.0'
b.frameworks = 'SystemConfiguration'.'CFNetwork'.'PassKit'
b.libraries = 'c++'.'z'
end
s.subspec 'C' do |c|
c.source_files = 'Classes/QSCore/C/**/*'
c.dependency 'Classes/QSCore/B'
end
end
Copy the code
Note: For an SDK with a lot of features, podSpec needs to do a lot of things, such as modularizing the code with subSpecs so that it can be imported on demand. Different modules may depend on the framework, static libraries, and other internal modules, which also need to be handled;
Note 2: You can use pod Spec lint xxx.podspec to check the validity of the PodSpec configuration
Podspec joins the private Sepc repo
- The public library posts. Podspec files to CocoaPods/Specs in trunk mode, and the internal pod component library is added to the private Spec repo on the terminal:
pod repo push qs_private_pods_specs QSBizSDK.podspec
Copy the code
Description: After qs_private_pods_specs is added, the podSpec information of the QSBizSDK library is included in the qs_private_pods_specs folder under ~/.cocoapods/repos. The remote side of the Git server is also updated.
5. More references
- Summary of Cocoapods principle
- Create a remote public library using CocoaPods
- CocoaPods creates a summary of public and private Pod library methods
- CocoaPods Private repository creation (in more detail)
Four, other
1, 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.
2, 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
3. Third-party libraries prohibit BITCODE
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_setting["ENABLE_BITCODE"] = 'NO'
end
end
end
Copy the code
4. Clear related commands
# Check the local POD cache
pod cache list
Clear a library cache
pod cache clean xxxx
Clear all POD caches
pod cache clean -all
Delete the cache method
rm ~/Library/Caches/Cocoapods/Pods/Pods/Release
Copy the code
5, other
Update library command
pod repo update
# Delete the search_index.json file
rm ~/Library/Caches/CocoaPods/search_index.json
Copy the code