Summary of CocoaPods’ Methods of Creating Public and Private Pod Libraries

  git, github, ios, objective-c, xcode

Introduction

At the time of graduation, some articles that came into contact with talked about iOS engineering modularization (cocopodization) everywhere. I have also been trying to decouple the company’s project codes. however, because it is really unnecessary for one person to develop engineering cocopodization, I have not studied the method of engineering cocopodization for a long time. during this period, I also took the time to attend the architecture exchange meetings held on platforms such as mushroom street. now the company’s business development is stable, and finally I have time toCreate public pod libraryMethods of making their own code available to other developers’ pod dependencies, andCreate a Private Pod Library for Local/IntranetFor the company’s project component CocoaPods method to study clearly, these days to sort out the notes to make a record.

The principle of creating a public Pod library or a private Pod library is actually the same, both are based on git service and repo protocol. The difference is that the version index query methods of the two are different, and the podspec of the public library consists ofCocoaPods/SpecsManagement, and the internal private use of pod library needs to establish a warehouse to manage podspec.

Text catalog
 I. Creating a Public Pod Library
 1. register CocoaPods account information
 2. Create a shared library file and upload it to the public warehouse
 3. edit. podspec file
 4. tag to release a Release version
 5. publish your own library description file podspec to cocoapods.
 6. About Finding and Using Newly Created Libraries
 7. update and maintain podspec.
 II. Creating Private Pod Library
 1. create a git warehouse to be used as Spec Repo for internal private library.
 2. Create a Private Pod Component Library
 3. add podspec to private sepcrpo.
 4. Find and Use Internal Component Libraries

I. Creating a Public Pod Library

Example: My First Public pod LibraryJM_ActionSheet, added to Podfilepod 'JM_ActionSheet'You can use it.

1. register CocoaPods account information

To create an open source pod library, first we need to register CocoaPods. Here we use trunk method. As an iOS developer, you must have installed CocoaPods, then you only need to execute it at the terminal:

Pod trunk register email address' username' --verbose

Here, we generally use github email and user name, then we will receive confirmation email in your email, click on the link in the browser to confirm that the registration is successful, and the terminal can execute after the success:

pod trunk me

Check your registration information. When you have your own open source Pod library, you can also check your published Pods at any time in this way.
图片描述

2. Create a shared library file and upload it to the public warehouse

Shared libraries require three essential parts:
A.Shared folder(The folder holds the content you want to share, that is, the files obtained by other pod. The source_files in the. podspec file needs to specify the file path and file type);
B.LICENSE file(MIT is generally selected by default);
C.Library description file. podspec(The information description of this library needs to be submitted to CocoaPods, pod can find your shared library through this file. See point 3 for the format of the podspec file).

This step is divided into two situations:
A if you already have a ready-made file that you want to share, you only need to satisfy the above three parts and upload it to the public warehouse to continue the other steps;
B if you want to create a new project to do your own sharing, you can use the terminal command:

Pod lib create library name

To create a standard project after selecting various configurations according to needs, please refer to:Using Pod Lib Create;

You can use tools such as SourceTree to upload your code to the public warehouse. For how to upload your code to the remote warehouse using the command line, you can move:IOS development and use of Git, which is not repeated here.

3. edit. podspec file

In order toJM_ActionSheet.podspecFor example:

Pod::Spec.new do |s|
 s.name         = 'JM_ActionSheet'
 s.version      = '1.0.1'
 s.summary      = 'An ActionSheet like WeChat'
 s.homepage     = 'https://github.com/ZJM6658/ActionSheetLikeWeChat'
 s.license      = 'MIT'
 s.authors      = {'JM Zhu' => '815187811@qq.com'}
 s.platform     = :ios, '6.0'
 s.source       = {:git =>  'https://github.com/ZJM6658/ActionSheetLikeWeChat.git' , :tag => s.version}
 s.source_files = 'JM_ActionSheet/*.{h,m}'
 s.requires_arc = true
 end

This is the simplest. podspec file style. The meaning of each attribute is clear at a glance. If this library depends on other libraries, plusS.dependency' library name'It is enough.

After the writing is completed, we need to verify the legality of the. podspec file. here, we need the folder where the terminal cd to. podspec file is located and execute:

pod lib lint JM_ActionSheet.podspec

If there are any warnings or errors, please recheck the correctness of your writing. If there are no problems, they will appear.

-> JM_ActionSheet (1.0.1)
 
 JM_ActionSheet passed validation.

4. tag to release a Release version

After everything is ready, we need to have a version in your git warehouse that is consistent with the. podspec file. here you can manually publish the releases item in your git warehouse or use terminal commands under the current folder:

git tag -m 'first release' '1.0.1'
 Gitpush-tag # push tag to remote warehouse

After success, you can see the tag version in your releases.

5. publish your own library description file podspec to cocoapods.

Also under this folder, the terminal performs:

pod trunk push JM_ActionSheet.podspec

Submit your library file. podspec file to the public specs. This step is to verify whether your podspec file is legal+submit to specs (equivalent to fork; commit; Push)+ Convert the uploaded podspec file into json format file), and after success, a Congrats message will appear oh ~
图片描述

6. About Finding and Using Newly Created Libraries

The waiting time after success is uncertain. Currently, it is generally relatively fast. Generally, pod setup and pod search are used to check whether they are ready for use. I will try every day for one week after creating this library.pod setupAndpod search JM_ActionSheetI can’t find my own library all the time, and find the solution after finding the information:

1. generated after successful podsetup~/Library/Caches/CocoaPods/search_index.jsonFile is an index file used for searching. The terminal inputs:

rm ~/Library/Caches/CocoaPods/search_index.json

Delete the search_index.json file in the ~/Library/Caches/CocoaPods directory and execute it after the deletion is successful:Pod search library name, waiting for output: creatingsearchindex for specrepo ‘master’ .. done! Wait a moment and you will get the result you want ~

7. update and maintain podspec.

If there are errors or iterative versions are needed, after the project files are modified and pushed to the remote warehouse, the version number in podspec needs to be modified, and tag uploaded again, then a new round of verification and release will be carried out. Of course, creating a demo DEMO project for other developers to download and view will not affect our pod library.

II. Creating Private Pod Library

There is no difference between the way private Pod libraries are created and the way public Pod libraries are created. The difference is that the spec repo that manages them is different.
So we need to create a heelCocoaPods/SpecsA similar warehouse manages the PodSPEC files of the internally created Pod library for internal personnel to update and rely on the internal POD component library.
Private repo is built in two forms, one is created on a private git server and the other is created locally.
Please refer to the official documents for the creation of this machine:Private Pods,
This article introduces how to create the whole service on the git server built in the company.

1. create a git warehouse to be used as Spec Repo for internal private library.

In a private server warehouse, a podspec is used to store all shared libraries. The internal SSH protocol address after being created here is:git@git.yaomaitong.net:iOSPods/ymtSpecs.gitThe same applies to the http/https address given to the private warehouse created by paying for git or using other free third-party git services (such as Bitbucket, etc.). the terminal enters the command:

pod repo add ymtSpecs git@git.yaomaitong.net:iOSPods/ymtSpecs.git

Add ymtSpecs to the local repo, and after successful addition, you can/.cocoapods/repos/The official specs:master and newly added specs:ymtSpecs can be seen in the directory.

If other cooperators use this private Spec Repo together, they can only add this Spec Repo by executing the same command under the premise that he has the right to correspond to Git warehouse.

2. Create a Private Pod Component Library

Continue to create a private warehouse to build internal components that need to be shared. take Category as an example: git @ git.yaomaitong.net: iospods/category.git, you can create a sample project and fill in your podspec file like creating a public library.

Pod::Spec.new do |s|
 s.name         = 'Category'
 s.version      = '1.0.1'
 s.summary      = 'ymt Category files'
 s.homepage     = 'http://git.yaomaitong.net/iOSPods/Category'
 s.license      = 'MIT'
 s.authors      = {'Gua Pi' => 'guapi@yaomaitong.cn'}
 s.platform     = :ios, '7.0'
 s.source       = {:git =>  'http://git.yaomaitong.net/iOSPods/Category.git' , :tag => s.version}
 s.source_files = 'AllCategory/*.{h,m}'
 s.requires_arc = true
 end

Note: homepage and source in podspec fileSsh protocol addresses are not supportedSo we have to put in the http/https address.

Like the way public libraries are created,pod lib lint Category.podspecAfter successful verification, push to the warehouse, then press tag to release the release version.

3. add podspec to private sepcrpo.

The public library uses trunk to publish. podspec files toCocoaPods/SpecsThe internal pod component library is added to the private Spec repo created in the first step and executed at the terminal:

pod repo push ymtSpecs Category.podspec

After successful addition, ymtSpecs will contain podspec information of Category library, which can be visited~/.cocoapods/reposAt the same time, the remote end in the git server has been updated.

4. Find and Use Internal Component Libraries

carry outpod search CategoryYou can find the Category library that you just created, and then add it to Podfile of the project that you want to use this component.pod 'Category', '~>1.0.1'You can use the internal components now!

It is worth noting that only when the git address source, pod install of your private Spec repo need to be added before Podfile can you find the private library in the private repo, like this:

source 'git@git.yaomaitong.net:iOSPods/ymtSpecs.git'
 
 platform :ios, '7.0'
 target "test" do
 pod 'Category', '~>1.0.1'
 end

After testing, this method can use Pod management to manage all your components that can be separated, even your business, thus achieving decoupling and single update optimization. Some components will not affect the dependent use of the old version. If problems occur, modifying the dependent version in Podfile can be rolled back at any time, which brings great convenience to development and is worth learning.

A minor update

Some students said that there was an error in using both private and public libraries. At this time, you can add public sources to Podfile:
source 'https://github.com/CocoaPods/Specs.git'

Reference link:
CocoaPods Guide