Mobile Development 13 min read

How to Build a Custom iOS Component Library with CocoaPods Scaffold

Learn how to manually create an iOS component library using CocoaPods, automate the process with the pod lib create scaffold, customize private templates, and develop a CocoaPods plugin to streamline library generation, complete with code examples and project structure details.

Zhongtong Tech
Zhongtong Tech
Zhongtong Tech
How to Build a Custom iOS Component Library with CocoaPods Scaffold

Manual Creation of an iOS Component Library Configuration

Componentized Development

As business requirements evolve, projects become larger and more complex, making component‑based development essential.

Componentization is the process of decoupling a complex system by splitting and recombining functional modules, providing an efficient way to handle complex applications and clarify module responsibilities. — Baidu Baike

CocoaPods offers a set of tools for managing component‑based development.

Steps to manually create an iOS component library with CocoaPods

Create an iOS project.

Add a configuration file (as shown below).

Write the component library code.

Publish the component library.

Pod::Spec.new do |s|
  s.name = "Test"
  s.version = '0.0.1'
  s.summary = "Test iOS component library configuration"
  s.homepage = "http://git.xxxx.com/UserName/Test"
  s.license = { :type => "MIT", :file => "LICENSE" }
  s.author = { "JPP" => "[email protected]" }
  s.platform = :ios, '9.0'
  s.source = { :git => "http://git.xxxx.com/UserName/Test.git", :tag => s.version }
  s.ios.deployment_target = '9.0'
  p.source_files = 'Test/**/*.{h,m}'
  p.framework = 'SystemConfiguration'
end

Manual configuration is cumbersome, especially for large projects that require many component libraries. A scaffold tool can automate repetitive tasks.

CocoaPods Scaffold for Component Library Configuration

CocoaPods provides a simple scaffold tool to create component library templates. pod lib create [YourProjectName] Example output:

aaron@AarondeMacBook-Pro TTT % pod lib create MyProject
[!] The specification of arguments as a string has been deprecated Pod::Command::Zto_template: `REPO_URL`
Cloning `https://github.com/CocoaPods/pod-template.git` into `MyProject`.
Configuring MyProject template.
---
To get you started we need to ask a few questions, this should only take a minute.
... (interactive prompts) ...
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
Ace! you're ready to go!
open 'MyProject/Example/MyProject.xcworkspace'

Generated project directory structure:

MyProject
  Podspec Metadata
    MyProject.podspec
  Example for MyProject
    ZTOAppDelegate.h
    ZTOAppDelegate.m
    Main.storyboard
    ZTOViewController.h
    ZTOViewController.m
    LaunchScreen.storyboard
    Images.xcassets
  Pods

Although the official template supports Objective‑C and Swift, it may not meet all project needs; custom templates can be created.

Custom Private CocoaPods Scaffold Tool

The CocoaPods scaffold supports custom templates. Migrate the official template to a private Git repository and modify scripts as needed.

pod lib create [YourProjectName] --template-url=http://git.xxxxx.com/UserName/pod-template.git

After migration, adjust scripts to fit your architecture.

CocoaPods Scaffold Source Code Analysis

Key directories of the official template:

configure      -- entry script
templates      -- template files for iOS projects
Pod            -- component code directory (renamed during generation)
setup          -- script directory
  ConfigureiOS.rb          -- Objective‑C iOS configuration script
  ConfigureMacOSSwift.rb  -- macOS Swift configuration script
  ConfigureSwift.rb        -- Swift iOS configuration script
  MessageBank.rb           -- console messages
  ProjectManipulator.rb    -- modifies Xcode project files
  TemplateConfigurator.rb  -- main entry script

configure

pod_name = ARGV.shift
Pod::TemplateConfigurator.new(pod_name).run

The script obtains the project name from the command line and runs the configurator.

TemplateConfigurator

Runs as the entry point, selects platform (iOS/macOS) and language (Swift/ObjC), then calls the appropriate configuration script.

ConfigureSwift.rb / ConfigureiOS.rb

Handles demo inclusion, unit‑test framework selection, and class‑prefix configuration for Objective‑C.

ProjectManipulator

Modifies Xcode project settings and template file names using the Xcodeproj library.

ZMAS Scaffold Template

The ZMAS template includes iOS native app, iOS component, and H5 app templates, supporting language configuration, Lego component libraries, and H5 homepages.

pod lib create [YourProjectName] --template-url=http://git.xxxx.com/UserName/pod-template.git

Building a CocoaPods Plugin for the Scaffold

Install the plugin framework: gem install cocoapods-plugins Create a plugin template: pod plugins create custom Core plugin code (simplified):

module Pod
  class Command
    class Custom < Command
      class Create < Custom
        self.summary = 'Project template'
        self.description = <<-DESC
          Project template, helps you quickly build a project or module.
        DESC
        self.arguments = [CLAide::Argument.new('URL', true)]
        def initialize(argv)
          @url = argv.shift_argument
          # parse URL to extract name and user
          super
          @additional_args = argv.remainder!
        end
        def validate!
          super
          help! '⚠️Git repository URL is required!!!' unless @url
        end
        def run
          clone_project_repo
          clone_template
          configure_template
          print_info
        end
        private
        def clone_project_repo
          UI.section("Downloading project from #{ @url }...") do
            git! ['clone', @url]
            UI.puts "🎉 Project download complete!"
          end
        end
        def clone_template
          UI.section("Downloading template from #{ template_repo_url }...") do
            `mkdir _TempDir_`
            git! ['clone', template_repo_url, '_TempDir_']
            UI.puts "🎉 Template download complete!"
            `mv _TempDir_/* #{ @name }/`
            `rm -rf _TempDir_`
          end
        end
        def configure_template
          UI.section("Configuring #{ @name } template...") do
            Dir.chdir(@name) do
              if File.exist?('configure')
                system({ 'COCOAPODS_VERSION' => Pod::VERSION }, './configure', @name, @zto_user_name, *@additional_args)
              else
                UI.warn '⚠️ configure script not found'
              end
            end
          end
        end
        def print_info
          UI.puts "For template issues see `#{ template_repo_url }`."
        end
        def template_repo_url
          TEMPLATE_REPO
        end
      end
    end
  end
end

Build and install the gem:

gem build cocoapods-custom.gemspec
gem install cocoapods-custom-1.0.0.gem --user-install
pod custom create [YourProjectGitRepo]

Platform‑wide management can be visualized using a Rails‑based demo (images below).

Rails demo overview
Rails demo overview
Template selection UI
Template selection UI
Generated project types
Generated project types

Source code reference: https://github.com/AaronYin0514/ios_template_platform

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mobile developmentiOSAutomationCocoaPodsComponent Library
Zhongtong Tech
Written by

Zhongtong Tech

Integrating industry and information for digital efficiency, advancing Zhongtong Express's high-quality development through digitalization. This is the public channel of Zhongtong's tech team, delivering internal tech insights, product news, job openings, and event updates. Stay tuned!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.