How to Write a Pod and publish it to CocoaPods?

Source: Internet
Author: User

How to Write a Pod and publish it to CocoaPods?

Cocoapods is a third-party library management tool developed by iOS.

This article describes how to write a pod by yourself, register it on CocoaPods, and put the code on Github for others to download. This is basically the "standard" mounting process for any pod. Of course, you can also put the pod on another git platform.

You can take a look at the pod and gitub address I wrote. If you think this article is helpful to you, start and fork are welcome ^

Preface

I have always wanted to write a pod to train the trainer, but I often cannot write it in half. The pod I wrote this time is extracted from a previous project, the implementation is not complex because the amount of code is small. I want to write a pod to understand the pod release process. Therefore, the focus of this article is not on the Code itself.

Preparations

This article assumes that the reader has a certain understanding of pod and git.
Git and pod should be installed properly. If not, go to Google. The pod version I used this time is v0.36, but the version has little impact on this article.

Start

When using xcode to write a pod, you should separate the Code as the pod from your example code in the directory (physical and logical), and specify the directory later When configuring the podspec file. In addition, it is best to add a special prefix to the project name. Otherwise, a duplicate name conflict may occur during pod registration.

1. Create a new repo on Github
The following operations are completed under terminal. 1) if the local code is not managed by git, cd to the project directory and execute git init. 2) git add origin 'github _ repo_url '// add the newly created repo url on github. 3) git remote-v // check whether the following items are successfully added. 4) git commit-am "init commmit" 5) git pull origin 6) git push origin
2. README. md

This file is very important for promoting your own pods. It is used to show the usage and function of your pods. Refer to the format of README files in my repo. This file is written in Markdown syntax. If you are not familiar with MD syntax, Google.

You may see a Gif animation in this file. Here we recommend a tool called LICEcap, which is a very small screen recording tool. After recording, remember to save it as a gif, put the saved git image in the project, and then point the path to gif in README.

3. podspec

At last, this step is where I encountered many pitfalls. When I checked the information, it was not very much, and it was basically in English.

First, create a podspec file in your project directory. The file naming method is as follows:podName.podspecPodName is the name of your pod and should be the same as the project name. The suffix is podspec.

First paste my own podspec file:

#CXLSlideList.podspecPod::Spec.new do |s|  s.name         = "CXLSlideList"  s.version      = "1.0.0"  s.summary      = "a light weight and easy to use tableview slide effect."  s.homepage     = "https://github.com/becomedragon/CXLSlideList"  s.license      = 'MIT'  s.author       = { "Becomedragon Cheng" => "becomedragonlong@gmail.com" }  s.platform     = :ios, "7.0"  s.ios.deployment_target = "7.0"  s.source       = { :git => "https://github.com/becomedragon/CXLSlideList.git", :tag => s.version}  s.source_files  = 'CXLSlideList/CXLSlideList/*.{h,m}'  s.requires_arc = trueend

The podspec file contains many parameters, but not all of them can be used. The parameters in this podspec file I wrote are basically enough. This file is written in the ruby language. If you have the knowledge of the ruby language, you should be familiar with this Code. Of course, it doesn't matter, because it is too easy to understand.

Next we will introduce each parameter one by one:
1. name: pod name, which must be consistent with your project name
2. version: version number. If you think it is the version number of your project, you are wrong. I am stuck in this place for a long time. Later I learned that this version is consistent with your branch name. If your branch name is 1.0.0, then this version can be written as 1.0.0. When the version is updated, to create a new branch, name it 1.0.1, and then write the version as 1.0.1.

3. summary: Describe your pod in one sentence
4. homepage: the url of the pod
5. license: the open-source protocol that your pod complies with, generally 'mit'
6. author: author name, contact information
7. platform: the platform and minimum system versions supported by the pod
8. ios. deployment_target: Same as above
9. source: pod address and tag
10. source_files: directory where the pod is located in the project. If there are multiple different directories, write them as follows: 'floder1... ', 'Floder2 .... ','.... '
11. requires_arc: whether it is ARC.

OK. Enter the podspec file carefully. It has already been more than half done.
Note that the. h. m file specified in source_files must not reference variables or header files in files other than source _ files. Otherwise, an error will occur later. Files in source _ files should be independent modules.

4. Register and upload CocoaPods

If you want others to passpod 'CXLSlideList'To reference your pod in this way, you must first register your pod on cocoapods. Of course, if the previous steps are correct, the registration declaration is a very simple process.

1. Register in cocoapods

Enter: pod trunk register YOUR_EMAIL 'your _ name' in terminal'

Then you will receive an email and click "OK.

2. verify the validity of the podspec File

Cd into your project directory, enter the following command: pod spec lint

This is to verify the validity of podspec, and compile the project according to the podspec file. Add-verbose after the command to see the execution steps that are believed to be performed.

This step can be frequent round-trip. If there is an error, there will be a prompt, it may be a compilation error, it may be another error, if it is a compilation error, you should look at your own code. Of course, after the code is changed, push it to github.

You may encounter an error called:
Cocould not find remote branch x. x. x to clone
If you encounter this problem, you still do not understand the version explanation I wrote earlier.

X. x. x is the version number you wrote in podspec. If it is 1.0.0, check whether you have created a branch named 1.0.0, push the branch 1.0.0 to github, and switch to the branch 1.0.0, repeat the preceding command.

After the preceding command is executed, no error or warning occurs. Otherwise, the next step cannot be completed.

3. push podspec

In this step, we can basically say that it is a success. Execute the following command to push podspec to cocoapods. If you are unfortunate enough to name podspec, cocoapods will prompt you that the name is occupied. I have never encountered this problem. It is estimated that the name can only be changed.

  pod trunk push PodName.podspec

OK. I will introduce it here. If you want to continue the study, you can use-help to view other commands.

Good luck & have fun ^

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.