How to create your own Pod?

Piotr Łopata on 19 July 2018

Everyone who comes into contact with programming for iOS or macOS has certainly heard the term CocoaPods. It’s a so-called dependency manager, which allows you to add libraries to your project in a simple and pleasant way. It is so popular that almost every public library is available through the use of CocoaPods. We often create our own components, extensions that we use in many projects. Maintaining such physical files is very inefficient. In addition, people who use the same components often make corrections, which is why nobody knows who has the most current version of the code (which increases the risk of potential errors in the application). The solution may be to create your own Pod, which will introduce order and automation (after entering one line of code all your extensions added to the Pod will be included in your project).

Creating your own Pod – step by step:

  1. Generating a template

To create a Pod template, CocoaPods has prepared a ready mechanism, which is activated with the following command:

After successful configuration, the project containing your Pod should open automatically.

  1. .podspec configuration

Then, fill out the information about your module:

  • Git configuration – first you have to think about the level of availability of your Pod to other programmers and determine whether your repository will be public or private. After creating the repository, fill in the addresses in the .podspec file and add your remote to the git created in the root directory
  • Language version – Swift 4.1 has been selected in the system configuration by default, so add the swift_version attribute, and fill out the summary
  • This is what the contents of your .podspec file should look like

  • Validation of the .podspec file:
    • To check whether you have configured everything correctly, enter the command
    • If you want to make your module public, it cannot contain any warnings from this validator (Xcode warnings are OK)

3. Extensions

The most important element of the process is adding your extensions to the project. Remember that the path to your source files in the .podspec file is set by default to the Classes folder (files added elsewhere will not be available in your project after installing the Pod. The path can be edited in the .podspec file in the source_files field)

After adding your file, the folder structure in the project should look like this:

Then add your code:

4. Creating a version of your Pod

Make sure that all your changes have been placed in your repository by entering the following commands:

Next, create a tag with the current version number of your module.

Publishing the pod on CocoaPods.org (public repository only):

If you have a problem with publishing the Pod, make sure that your session is configured correctly

Your page with the Pod repository should look like this:

  1. Pod installation

If you haven’t yet configured the Pods in the project, you can quickly do so by entering the command:

while in the main folder containing your project file.

Sample content of Podfile file:

After configuration, you can add a line that will be responsible for adding your Pod to the project.

If it is private, you need to add the address of your repository

Then in the terminal enter

After successful installation, the Pod is available in your project 🙂

Remember that using CocoaPods, the project is initiated with the .xcworkspace file – if you use .xcodeproj, your project will not build!

Now just import your module:

and you can use it:

output:

Finished! Your Pod is properly created and fully configured.

Environment:

CocoaPods – 1.4.0
Xcode 9.4.1
Swift 4.1
macOS 10.13.5

 

Tagi: , , , , , ,

You might also like

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Appchance web and mobile app services
Check how to flourish
your business using
mobile app
I have read and accepted the terms of use.