Creating custom frameworks in iOS and publishing them via CocoaPods is a valuable skill for iOS developers. This comprehensive guide will walk you through the entire process, from creating a custom framework to making it available through CocoaPods.
Prerequisites
Before you begin, ensure you have the following prerequisites:
1. Xcode: You need Xcode, Apple's integrated development environment for macOS and iOS development.
2. CocoaPods: Make sure you have CocoaPods installed. If it's not installed, you can do so by running:
sudo gem install cocoapods
Step 1: Create a New Xcode Framework Project
1. Open Xcode and select "File" > "New" > "Project."
2. Choose the "iOS" template for "Framework" under "iOS."

3. Provide a name for your framework, and select the language (usually Swift).
4. Click "Create."

Step 2: Configure Your Framework
1. In the project navigator, find the root folder of your framework.
2. Create Swift or Objective-C source files and add your code. This is where you define the functionality you want to encapsulate within your framework.
3. Make sure to declare all your public interfaces in the framework's public headers. These headers should be accessible to other projects using your framework.
4. Organize your code and resources as necessary.

Step 3: Build Your Framework
1. To build your framework, select the scheme for your framework target (it should have the same name as your framework).
2. Choose a destination, such as "Generic iOS Device" or the iOS simulator.
3. Click the "Build" button or use the keyboard shortcut "Command + B."

Step 4: Locate Your Framework
1. After building your framework, locate the framework file. It is usually within the "Products" group in the "Project navigator."
2. Right-click the framework and select "Show in Finder." You'll find the framework in a directory with a .framework extension.


Step 5: Create a GitHub Repository
Push your custom framework to a GitHub repository so that it can be easily accessed by CocoaPods.
1. Initialize a Git repository in your framework's directory: git init
git init2. Add your files to the repository:
git add.3. Commit your changes:
git commit -m "Initial commit"4. Create a new repository on GitHub, and follow the instructions to push your code to GitHub:

git remote add origin https://github.com/yourusername/yourframework.git
git branch -M main
git push -u origin main
Step 6: Publish Your Framework to CocoaPods
- Ensure you have a CocoaPods account and are logged in via the `pod trunk register` command.
pod trunk register
//example
pod trunk register manikanta.sirumalla@outlook.com 'Manikanta Sirumalla' -- description='macbook pro' 2. In your framework's directory, run the following command to create and publish your Podspec:
pod spec create YourFramework
//example
pod spec create CredentialValidator
This command will create a `.podspec` file that you can edit.
Step 7: Create a Podspec
A Podspec file describes your framework to CocoaPods. Create a `.podspec` file with your framework's details. Here's an example Podspec file:
Pod::Spec.new do |s|
s.name = 'YourFramework'
s.version = '1.0.0'
s.summary = 'A brief description of YourFramework.'
s.description = 'A more detailed description of YourFramework.'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.homepage = 'https://github.com/yourusername/yourframework'
s.author = { 'Your Name' => 'your@email.com' }
s.platform = :ios, '11.0'
s.source = { :git => 'https://github.com/yourusername/yourframework.git', :tag => s.version.to_s }
s.source_files = 'YourFramework/*.{h,swift}'
end
Ensure you replace placeholders like 'YourFramework,' 'yourusername,' and other relevant details with your actual information.
1. Open the Podspec file and modify it as needed to match your framework's details.

2. Validate your Podspec:
pod lib lint YourFramework.podspec
//example
pod lib lint CredentialValidator.podspec3. If the validation is successful, push your Podspec to CocoaPods:
pod trunk push YourFramework.podspec
//example
pod trunk push CredentialValidator.podspec
Step 8: Using Your Custom Framework
To use your custom framework in an iOS project:
- Create a new Project and Open the project where you want to use the framework.
2. Add the framework as a dependency in your project's `Podfile`.

3. Run `pod install` to fetch and integrate the framework into your project.
pod install4. You can now import and use your custom framework in your app.
//
// ViewController.swift
// Test
//
// Created by Manikanta Sirumalla on 26/08/23.
//
import UIKit
import CredentialValidator
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
CredentialValidator.sayHello()
print(CredentialValidator.validEmail("test123@xyz.com"))
}
}Congratulations! You've successfully created a custom framework and made it available via CocoaPods. This modular approach allows you to share code across multiple projects and with the iOS development community.