'Create your own cocoa pod

I have checked several reference links and questions about how we can create the cocoa pods, tried:

None of these provide complete information where I can get all the steps to create my own cocoapod.



Solution 1:[1]

How To Create Own Pod

Create Custom Library in Swift

Overview

Create Cocoapods Library Swift: This post is referring to crate own pod that is custom cocoapod library using swift. Xcode allows to create own cocoapods library using swift, these means creating own iOS framework which will be globally available for other developer. Developers can use this custom pod for their project by installing this as a third party library.

Create Custom Pod: To create own custom pod and to make it publicly available for other developers we will going to use Git Hub. As the Git Hub is the best to medium to host our custom pod and to make it publicly available. Also Git Hub provided version controlling for managing different versions of custom pod. Hence when user wants to update their custom pod library, they can easily do this by creating new release and by updating their custom pod library version.

Create your own library in swift & make it available globally. Custom pod as in own Cocoapods library. Distributing own pod (own library) to the iOS Open Source Community. Already there are many different open source (pod) libraries available on git, like Alamofire, SwiftyJSON and more. In this article we are going to learn, how to create an own pod (library) with the following steps -

In this article we are going to through following steps

Requirements

  • Xcode 6 or above
  • Swift 4
  • iOS 9 or above
  • Git hub account or repository

Steps

  1. Create a new public repository
  2. Give a name to repository and add description
  3. Check for initialising with README
  4. Select license for MIT license
  5. Click on ‘Create Repository’ to get started
  6. Cloning Repository
  7. Click on ‘Clone or Download button'
  8. Select Clone with HTTPS url & copy that url
  9. Open new terminal window & select project folder to clone
  10. After cloning there should files README.md & Licence file available
  11. Start Xcode with File -> New -> Project -> Framework
  12. Give a name of Product, Select language to Swift & click to Next button
  13. Now select the same folder which is used for cloning repository and save the project
  14. Create a new swift file: RClick on project folder and select ‘New File’ -> select swift file & give a name to the file.
  15. In this new file write down the require code for creating custom pod. 16. Depending the requirement you can create as many number of file as much you want. Remember to add only require files & require amount of code to maintain the library file size & its integrity. Check ‘Notes’ section below for more info.
  16. Now save the code & close the project as it is.
  17. And you can push these all source code into Git hub.
  18. Open terminal window & change directory to your library project folder & write & hit ‘return’

‘pod init’

  1. Also you need to add .podspec file. This files contains information about your custom pod library. After creating Podfile in the project now add .podspec file using following command, remember to give your repository name in the following command

pod spec create YOURRESPOSITORYNAME

  1. Now you must have see ‘Podfile’ & ‘YOURRESPOSITORYNAME.podspec’ files into your project folder, you can check the availability of these files from finder itself or by listing the folder contents from the terminal.

  2. Next you need to open ‘YOURRESPOSITORYNAME.podspec’ file in any of your favourite editor. Rather you can select Xcode as an editor to this file. From terminal you can enter following command to open it in Xcode or you can RClick from finder & select Xcode as editor

open YOURRESPOSITORYNAME.podspec -a Xcode

  1. Here you need to edit the ‘YOURRESPOSITORYNAME.podspec’ file. Actually you need to specify all necessary detail about your custom pod library of iOS framework.

  2. After completion of editing ‘YOURRESPOSITORYNAME.podspec’ file, close this file (file editor) and move to terminal window. Make sure you are in same path (same directory) of your custom pod folder that contains your ‘YOURRESPOSITORYNAME.podspec’ file. Here write following command & hit return

pod spec lint

This command will validate your custom pod with all pod validation properties. There are different validation steps for validating you pod, this validation may get failed at any of the validation steps. Make sure all of the validations steps should get passed without warnings.

  1. Also sometime while validating custom pod, may get failed due and you ended up with an warning of no licence file attached with repository although you added the license, these validation sometimes failed to get the license. In such case you can write (--allow-warnings) command like below -

pod spec lint -- allow-warnings

  1. The validation process takes some time to complete. After completion of the validation process, you will get message in green text colour.

YOURRESPOSITORYNAME.podspec passed validation

  1. Now your custom pod is available to use, you can test this pod on any separate project by providing local path in the pod file

pod ‘YOURPODNAME’, :path => “YOURLOCALCUSTOMPODPATH”

  1. After successfully completion of testing, here you have to push all of the source code of your custom pod project to the Git Hub. This will be your very first version of your custom pod.

  2. Once you push all source code to git, then you need to make this custom pod available globally & for this firstly check for session using

pod trunk me

This will displays the information about the current running session

  1. Next setup with trunk by registering your custom pod. Enter following command & hit return.

pod trunk register YOUREMAILADDRESS ‘YOUR NAME’ — description=’macbook’

Remember, in the description above after equal too sign & inside quotes just write which type of macbook you have, either ‘macbook pro’ or ‘macbook air’

  1. This will show up an message for asking you verify your provided email address. For this you need to open your provided email & find for the confirmation email that you get from Cocoapods. Here you will get the link, click on this link & you will move to official cocoapods page will be showing you completion of your custom pod setup.

  2. Lastly, return back to the terminal & write following command to complete custom pod process

pod trunk push

This will again validate your custom pod & checking all necessary files. After this checking it will be published & you will congratulations message. This confirms that your custom pod is now available globally.

How to Update Custom Pod


Steps:

  1. First Do all the necessary changes into your custom pod project and save the project file (if not autosave enable).
  2. Push all these changes into git repository connected with your custom pod project with necessary commit and then check repository for your recent commit.
  3. Now open your .podspec file and to go the version line and update your custom pod version as per the requirement. (Note no. 4)
  4. Commit the .podspec version update into git with require commit.
  5. Next is move to Terminal and change directory to your custom pod folder which contains .podspec file and write following command

pod spec lint

  1. After successful lint of the custom pod, go to your git repository (from browser) and click to release option to create a new release for these new changes

  2. Click on ‘Draft a new release’, enter require tag version, give a release title, write any description and click on ‘Publish Release’ button to publish this new release. (Note no. 5)

  3. Once your release get publish successfully, move to terminal and on same directory (contains .podspec file) enter following

pod trunk push

  1. After successfully pushing the files, your terminal window will show an congratulation message with custom pod name & its new version number.

  2. Now you can check this new version of your custom pod by moving to your project root folder into terminal and enter following to update the pod with your current new version.

pod update

** Notes:**

  1. Always try to write only necessary code for the library. Large number of lines of code increase the size of the library. Try to make it as small and as compact as you can.
  2. Keep the source code tidy within the file, never try to increase the complexity of the code. This should be easy for user to understand & easily access within the project.
  3. Try to add comments or comments mark to specify which code is used for what purpose. You can also add start code comment & end code comment for specifying the particularity of the source code.
  4. While update the custom pod with new version, always check that the old version of the same custom pod should not get update. Means never try to update old custom pod version with new changes, always update your custom pod version number although there are some minor changes into it.
  5. You can also select particular branch from your repository to draft a new release from. Only the condition is your new changes should be available in the branch that you going to select for drafting new release.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1