First, establish a dependency library

Dependency libraries are libraries that store third-party code, and Cocoapods is essentially a convenience tool for third-party library maintenance and import. Dependency libraries, also called POD libraries, are generally published on public code hosting sites for others to download and use, such as AF on Github, MJRefesh, etc.

In many cases, companies do not want to leak code, but need to use a componentized framework, this time will publish their code to a private repository, such A POD library is generally called private library. Its essence is no different from AF, MJRefesh, etc. The only difference is that one is open and the other is not.

1. Create a project in Git

In general, dependency libraries can be created directly using public class servers such as github’s repository or private servers such as GitLab. Gitlab is used here as a demonstration

2. Use Git to manage projects

Add a new repository using sourceTree:

3. Upload the code to the dependency library

If readMe initialization is selected, there is a readme.md file in the project. In this step, you just need to assign the code to the change project and submit it. Here, modify the readme.md file to demonstrate:

Create a Spec repository

The concept of a Spec warehouse is equivalent to a map that can be understood as a list of items. Since there are many POD libraries, each pod library will have its own address for storing code, it is impractical and inconvenient to import many pod libraries and add addresses to each podfile.

Spec warehouse is to store all the pod of the library code address and information, in the use of specific pod library, directly in the warehouse to find the download address to download the corresponding code, and according to the pod library some of the configuration information for the corresponding configuration, all of these processes is realized automation scripts, key documents are: Podfile, spec file, and spec repository.

When we use Cocoapods, we download the Spec repository for the first Pod Update. The repository is large, so the first time is slow. The essence is to download github.com/CocoaPods/S… In addition, the contents of the spec library can be found directly on our computer. The specific address of the spec library is not posted, so it is easy to find.

1. Create a Spec repository on Git

This step is the same as the previous step in creating the dependency library, except that the only purpose of the spec library is to store the corresponding specs for the private library. In pod, the source needs to set the address corresponding to the spec, which is the address of the repository that is now being created. The only difference between a common library (AF, SDWebimage, etc.) and a common library (AF, SDWebimage, etc.) is that the spec file for a common library is stored in the Master branch of Cocoapod, and the spec file for a private library is stored in a private repository.

2. Use Git to manage the Spec repository

Similarly, use sourceTree to pull directly, omit.

Integration links between dependency libraries and spec libraries

1. The dependency library generates a spec file

Command to initialize spec:

pod spec create specName
Copy the code

2. Configure the spec file

Basic format:

Pod::Spec.new do |s|
...
end
Copy the code

1. Fill in the spec Metadata

  s.name         = "XKQuote"
  s.version      = "0.0.1"
  s.summary      = "the summary of Spec."
  s.description  = "description of the Spec."
  s.homepage     = "http://EXAMPLE/XKQuote"
Copy the code

2.Author Metadata

  s.author             = { "caoxk"= >"[email protected]" }
Copy the code

Note: The author and email address must be the same as the following License. 3.Spec License format:

  s.license      = { :type= >"MIT", :file => "LICENSE" }
Copy the code

MIT License is usually used:

Note: A license can be understood as a copyright message, or even as a simple string of strings. You can either create an empty file and copy the string, or create a Specifics on GitLab.

s.platform     = :ios, "8.0"

  # When using multiple platforms
  # s.i OS. Deployment_target = "5.0"
  # s.o sx. Deployment_target = "10.7"
  # s. atchos. Deployment_target = "2.0"
  # s.t. Vos deployment_target = "9.0"
Copy the code

5.Source Location

  s.source       = { :git => "http://EXAMPLE/XKQuote.git", :tag => "#{s.version}" }
Copy the code

Supported keys: :git => :tag, :branch, :commit, :submodules :svn => :folder, :tag, :revision :hg => :revision :http => :flatten, :type, :sha256, :sha1 Git, SVN, Hg, and HTTP are the source channels. Also, “URL address”, which is the default key. In our example, the address of the dependent library in GitLab can be filled in:

  s.source       = { :git => "https://gitlab.com/caoxk289722789/xkquote", :tag => "#{s.version}" }
Copy the code

Remark:

  • Source has nothing to do with homePage. HomePage is only an unnecessary information that the author tells the reader (in fact, the most important Metadata are name, version, and author).
  • s.version == s.version.to_s ??? 6. The Source Code option, which tells Cocoapods what syntax you want to copy into your project:
// Source file (file to copy) s.source_files ="Classes"."Classes/**/*.{h,m}"// Excluded files s.exclude_files ="Classes/Exclude"// header files (if not, all header files will be exposed, try again)"Classes/**/*.h"
Copy the code

Example: File structure:

Writing:

  s.source_files  = "QLCrashGuardDemo/QLCrashGuardSupportFiles/**/*.{h,m}"
Copy the code

This example uses the README directly to demonstrate:

  s.source_files  = "SourceFiles/*.{h,m}"
#s.exclude_files = "Classes/Exclude"
#s.public_header_files = "Classes/**/*.h"
Copy the code

Note: * : indicates files in all formats 7.Resources is used to add resource files such as images 8.Project Linking is used to add framework 9

3. Verify the SPEC file

Command:

Pod Spec Lint (needs to run in the corresponding path)Copy the code

As shown in figure:

Note 1. When adding a tag, push it to the remote end; otherwise, tag 2 corresponding to version still cannot be found. If the tag is pushed in time, sometimes the update may not be timely due to cache reasons. In this case, add a new tag and then verify

According to the template to fill The official document: guides.cocoapods.org/syntax/pods… A few key points:

4. Add the Spec repository to the local PC

This step is to download the Spec repository locally. This means that on the first Pod update, cocoapods will automatically download github.com/CocoaPods/S… This time we manually downloaded our own Spec index to the local.

Command:

Pod repo add specFileName(name of spec repository)Copy the code

Execute command:

pod repo add XKQuote https://gitlab.com/caoxk289722789/xkquotespec
Cloning spec repo `XKQuote` from `https://gitlab.com/caoxk289722789/xkquotespec`
Copy the code

SpecFileName is the same as the folder name, and cocoapods is the master folder, so it doesn’t matter what specFileName is set to here;

5. Push the spec file of the dependent library to the Spec repository

We have a local spec index, but the spec index does not have any information about the private library that we are going to use, so we need to push the podspec file of our private library into the spec library to create the index. In this way, we can first go to the Spec library to find the private library, and then extract the private library podSpec file stored in the file, use the source address in the file to download the source code, and according to the configuration commands in the PodSpec file to configure the source code and project. We are now ready to use our own private library.

The command is as follows:

Pod repo push XKQuote xkquot.podspecCopy the code

Add and push and you will see the following:

If you want to push to a public library, which is the Spec repository of cocopods on Github, you can use the trunk command as follows:

pod trunk push XXX.podspec
Copy the code

Import dependency libraries into the project

Note: 1. Add the address of the corresponding spec repo; otherwise, the corresponding dependent library cannot be found. 2

Successful Results of POD:

Use local path in pod:

Such as:

    pod 'BaseBusiness', :path => '.. /.. /BaseBusiness'
Copy the code

There are several features: 1. There is no local spec repo, but it is based on the corresponding spec in the local path. 2. Direct use of local source_files set files, do not copy, will create an index, so in the project after the modification will affect the source files, if there is git, there will be git association, so it is suitable for development stage to use.

Summary: The essence of Cocoapod is divided into two warehouses, specifically divided into quantity warehouse. It can be visually understood that there are many warehouses in the harbor, one is a warehouse for storing goods, and in an office, there is a list of information about these goods. The dependency repository is the cargo repository, and the Spec Repo is the manifest repository. For each POD update, first download the goods list according to the source, then search for the corresponding spec according to the specific goods required in POD XXX, and then conduct POD after understanding the information and configuration of the goods. In fact, what is the operation of POD? See project, Workspace, libary, and Framwork in the next chapter of ios for details