CocoaPods is a collaborative platform for developers utilizing Xcode to develop iOS applications. When working on your project, you can leverage pre-existing code from other projects or developers to enhance efficiency and save time; instead of manually researching how to integrate these pieces of code, CocoaPods steps in.

A podfile is a Ruby script written in a declarative DSL (domain-specific language) that defines the dependencies for your project. It specifies the external libraries or frameworks your project needs.

Table of Content

How to Generate Podfile

  • Open Terminal, navigate to the project directory, and add the command "pod init" to generate a podfile.
  • Save the Podfile in the root directory of your Xcode project.
  • Add your library name into the Podfile as "pod library name."
  • Run the “pod install” command to install the specified dependencies.

The main benefit? You can use powerful features without the hassle of setting them up. It is like having a friend who finds the best tools for your job and makes sure they work smoothly on your project.

CocoaPods, an effective iOS dependency manager, typically integrates open-source libraries sourced from public and private repositories. However, there are situations where creating private CocoaPods libraries is beneficial. These private libraries provide enhanced code security, streamlined collaboration, version control, and the ability to customize solutions for specific projects.

CocoaPods is built with Ruby or Brew; it will be installable with the default Ruby or Brew available on macOS.

$ sudo gem install cocoapods

OR

$ brew install cocoapods

CocoaPods is like a project manager for your iOS app development. It takes care of the process when you want to bring in external code libraries to enhance your app. CocoaPods grabs that code and figures out if it needs any other pieces (dependencies) to work correctly. It is like assembling a team of superheroes for your project.

CocoaPods knows how to talk to different code storage systems (like git, svn, etc.), so it can fetch the source code from wherever it is stored. Once it has everything it needs, it rearranges your Xcode project and creates a special workspace for it. This workspace includes your original project, the external code, and a special project just for managing these external bits (the Pods project).

CTA - 1.png

CocoaPods lets you decide how much assistance you want by running a specific command during installation. It is like having a personal assistant for your project, making things easier but letting you take the reins if you want to.

Create your Library through Terminal Commands

Step 1: Ensure that the folder for your library is blank and open a terminal within that directory.

Step 2: Use the command below to initiate the CocoaPods library creation process.

pod lib create <POD_NAME>

Step 3: Follow the question-and-answer prompts provided by the CocoaPods setup process, as shown in the accompanying screenshot.

2.1.webp

Set up a Private Repository for your Custom Cocoa Pods Library

Step 1: Create an Account on GitHub, GitLab, or Bitbucket to create a new private repository.

Step 2: Create a new private repository, ensuring it is marked as Private. You can provide a unique library name of your choice. Alternatively, you can use Xcode to create a new Private repository.

GitHub:

3.1.webp

Xcode:

3.2.webp

Configure GIT in your Xcode for Version Control with the Library

Step 1: After creating the Xcode project using the previously mentioned command, open the terminal within the project directory and make the following changes:

XCode Settings:

  • iOS Deployment Target: 12.0
  • Swift Version: Swift 5
  • Project Format: XCode 12-compatible

Podspec File Updates:

Code in podspec filePurpose
s.swift_version = '5.0'Compatible version of swift
Classes folderDelete replace me file and add classes folder
s.resource_bundles = { 'librayname' => ['librayname/**'] }Access storyboard
s.frameworks = 'UIKit', 'Foundation'Access all controllers' liker label, text field
s.dependency 'AFNetworking', '~> 2.3'Access third party library’s

Step 2: Place them in the 'assets' folder. You do not need to create a new folder for assets.

4.1.webp

Step 3: Provide the framework bundle path for accessing images from code.

Step 4: Commit the changes and push them to the remote repository.

Step 5: Tag the version, Same as the podspec version mentioned, and push them to the remote repository.

Step 6: Release the tag version at the remote repository. As mentioned below:

4.2.webp

Set up the terminal for smooth library validation and publishing private CocoaPods.

Step 1: Add the mentioned command to verify the account.

pod trunk register <EMAIL> 'Author Name' --description=<USER_NAME>

Step 2: Open your email account, click on the verification email, and verify a link for creation following the library.

pod trunk me

Step 3: Provide the framework bundle path for accessing images from code.

pod lib lint <POD_NAME>.podspec --allow-warnings

5.1.webp

Step 4: Open a terminal from the library directory and publish the library

pod trunk push <POD_NAME>.podspec

Step 5: If you are facing an error due to multiple GitHub accounts or need to manage your credentials, consider these actions:

Remove all associated email and password entries from Keychain.

Access and other relevant locations.

Ensure your multiple GitHub accounts are correctly configured to avoid conflicts or credential issues.

Step 6: Add repo private library

pod repo add <POD_NAME> <URL>.git

Step 7: Publish Private Library

pod repo push <POD_NAME> <POD_NAME>.podspec --allow-warnings

5.2.webp

Integrate your Private Library into other iOS Swift Projects for Code Reuse

Step 1: Create a new Xcode project open terminal within the project directory folder and add the mentioned command

pod init

Step 2: Open the generated pod file, add a source URL, and pod name with tag or version as mentioned

Step 3: Update pod using the tag version as mentioned in the below screenshot

6.1.webp

Step 4: Install Pods

6.2.webp

Conclusion

This guide illustrates creating a private CocoaPods library for iOS using Swift, enhancing project code management. By adhering to these instructions, developers can effectively build and manage their proprietary code, promoting a structured and controlled environment for project dependency management.

So, what are you waiting for? Empower your business with our top-notch iPhone app development services and bring expertise to the table, ensuring your app stands out in the competitive market. Hire The One Technologies to bring your vision to life, delivering seamless, high-performance applications tailored to your needs.

Logo of E-Static Infotech