Cocopods是iOS上常用的负责管理framework的工具

Cocopods是如何工作的

分为两部分介绍

  1. 在App的项目里安装framework
  2. 在Cocopods发布framework
1. 在iOS项目里安装framework

创建Podfile文件,里面要告诉Cocopods需要安装的framework名称,例如

1
2
3
target 'MyApp' do
pod 'FrameworkA'
end

意思是要往MyApp项目里安装FrameworkA

完成配置后,要执行pod install执行安装步骤

pod install会读取Podfile里的内容,执行相应步骤

Cocopods会从远程仓库查找相应的framework进行下载,链接到项目

注意,如果不指定版本,Cocopods会自动下载最新版本

执行完pod install后,Cocopods会自动生成workspace文件,并把目标framework链接到项目中

2. 在Cocopods发布framework

这只发布的流程示例

大体分为这几个步骤

  1. 安装Cocopods
  2. 为framework创建git仓库
  3. 发布framework到git仓库
  4. 项目安装framework
  • 安装Cocopods

    1
    gem install cocoapods
  • 为framework创建git仓库

    先创建一个本地仓库

    1
    2
    3
    mkdir ~/MyFramework.git
    cd ~/MyFramework.git
    git init --bare

    把MyFramework的项目内容绑定到刚才的git仓库

    1
    2
    3
    4
    5
    6
    7
    8
    cd ~/MyFramework
    git init
    git remote add origin ~/MyFramework.git
    git add .
    git commit -m "Initial commit"
    git tag -a 0.0.1 -m "Version 0.0.1"
    git push origin -u master
    git push origin --tags
  • 创建本地指定的pod仓库

    仓库用来发布自定义的framework,执行下列操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    mkdir ~/MySpecs.git
    cd ~/MySpecs.git
    git init --bare
    git clone ~/MySpecs.git ~/MySpecs
    cd ~/MySpecs
    touch README.md
    git add README.md
    git commit -m "Initial commit"
    git push origin -u master
    pod repo add my-specs ~/MySpecs.git
    #~/MySpecs.git 使用用绝对路径

    完成后执行

    1
    pod repo list

    定义的my-specs的pod源就被添加进来了

  • 添加MyFramework的pod配置文件

    执行操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    cat > MyFramework.podspec <<-EOF
    Pod::Spec.new do |s|
    s.name = "MyFramework"
    s.version = "0.0.1"
    s.summary = "A brief description of MyFramework project."
    s.description = <<-DESC
    An extended description of MyFramework project.
    DESC
    s.homepage = "http://your.homepage/here"
    s.license = { :type => 'Copyright', :text => <<-LICENSE
    Copyright 2018
    Permission is granted to...
    LICENSE
    }
    s.author = { "$(git config user.name)" => "$(git config user.email)" }
    s.source = { :git => "$HOME/MyFramework.git", :tag => "#{s.version}" } #你的Framework的路径
    s.source_files = "MyFramework/**/*.swift"
    s.resources = "MyFramework/**/*.xib"
    s.platform = :ios
    s.swift_version = "4.2"
    s.ios.deployment_target = '12.0'
    end
    EOF

    完成后,上传pod配置文件到repo

    1
    pod repo push my-specs MyFramework.podspec

cocpods的全部流程已经完成

​ 之后就可以在项目里的Podfile添加Myframework的库了

1
2
3
4
5
target 'MyApp' do
use_frameworks!
pod 'MyFramework', '0.0.1', :source => "$HOME/MySpecs.git"
end
EOF

需要标明use_frameworks!

source指定MySpecs.git位置

https://medium.com/onfido-tech/distributing-swift-frameworks-via-cocoapods-152002b41783