Charlene
Assistant Engineer
Assistant Engineer
  • UID626
  • Fans1
  • Follows1
  • Posts53
Reads:3047Replies:0

Add CocoaPods support for code on GitHub

Created#
More Posted time:Nov 15, 2016 16:45 PM


I. Preface
I’ve been physically and mentally exhausted by CocoaPods these days. The official website of CocoaPods provides instructions on how to add the support, but because of its oversimple descriptions (subtitle: my poor English), and some skipped steps, I didn’t succeed referring to the official documents. I later checked the jianshu.com and CocoaChina among others, and was not too far from breaking down. There is just no complete introduction - none. So I simply compared the multiple documents, ran tests and finally managed to successfully enable my repository on Github to support CocoaPods installation.


II. Start
Here I will give introduction from the very beginning, including creating projects on GitHub, uploading projects and finally adding support for CocoaPods.
Steps are as follows:
• Upload code to GitHub
• Create the podspec file and validate whether it succeeds
• Create the release version on GitHub
• Register a CocoaPods account
• Upload code to CocoaPods
• Check whether the upload is successful
1. Upload code to GitHub
First, we open the github.com, and create a project:


Here pay attention to the MIT License. It will be used for adding the CocoaPods support in the later part (to be described later). Then click Create.
Download the code to a local place using SouceTree and put your project into it. The folder structure is shown in the figure:


Here the LICENSE is the file added by the MIT License just mentioned. RSADemo is a demo project, and ZGRSAEncryptor is the repository it provides for others’ use. (ZGEncrptor(For java).zip is the backend code)
Then you can submit it to GitHub.
2. Create the podspec file
Access the project directory using the terminal:


Run the following commands:
pod spec create ZGRSAEncryptor
Here the ZGRSAEncryptor is the name when the pod was added (such as MBProgressHUD). The result after execution:



At this time, a ZGRSAEncryptor.podspec file will be added in the project folder. Here I use Sublime Text to open and edit it as follows:
Pod::Spec.new do |s|
  s.name         = "ZGRSAEncryptor"
  s.version      = "1.0.0"
  s.summary      = "A Library for iOS to use for RSA encryptor."
  s.homepage     = "https://github.com/ScottZg/ZGRSAEncryptor"
  s.license      = "MIT"
  s.author             = { "scottzg" => "scottzg@126.com" }
  s.source       = { :git => "https://github.com/ScottZg/ZGRSAEncryptor.git", :tag => "#{s.version}" }
  s.source_files  = "ZGRSAEncryptor/*.{h,m}"
end


name: the class library name. Descriptions of the fields are as follows:
version: the library version
summary: the introduction, that is:


homepage: the project address on the GitHub
license: the license
author: the author
source: the HTTPS address of the project
source_files: the code to be shared. Here it is all the code under ZGRSAEncryptor.
Next we will execute the following commands for verification:
pod lib lint ZGRSAEncryptor.podspec
The results may come in varied forms. In case of any errors, just rectify the error as prompted. The result of my execution is shown in the following figure:


A warning is added. It is fine as long as it is not an error. The warning can be ignored directly (the red texts also prompt how to ignore the warning):
pod lib lint ZGRSAEncryptor.podspec —allow-warnings
The result is as follows:


When you see “ZGRSAEncryptor passed validation”, it indicates the project has passed the validation.
3. Create the release version on GitHub
Open the project directory and create the class library of the release version:


Click as the arrow points to start creating the release version (click Create a new release):


Click Publish release. After creation is done, the page will be displayed as shown in the figure:


So the third step is completed.
4. Register a CocoaPods account
Execute the command line:
pod trunk register E-mail address ‘User Name’ —description=‘Descriptive information’
After execution, the result is as follows:


The yellow text indicates a verification code has been sent to the e-mail address. You can open your e-mail box to verify it. The link in the e-mail is shown as follows:


So you have successfully registered a CocoaPods account.
You can run
pod trunk me
to check whether the creation is successful. If it is a success, the result as below will be shown:


5. Upload code to CocoaPods
First, check the validity of the file format:
pod spec lint
The result is as follows:


No errors, but with a warning. You can use —allow-warnings to ignore warnings:


If the text “passed validation” is displayed, it means the validation is passed. Then execute:
pod trunk push ZGRSAEncryptor.podspec —allow-warnings
The execution results are as follows: (The speed may be slow)


It indicates the uploading has been successful.
6. Check whether the upload is successful
Run
pod search ZGRSAEncryptor
The result is as follows:


Okay. It means the uploading has been successful. In this way, others can search for and use it.
Guest