iOS组件化: 如何让你的代码支持pod?

750 阅读3分钟

最近看了一下组件化,照着文章结束的那篇博客操作了一遍,这是中间碰到的问题以及如何解决。

创建模板

cd 到一个本地文件夹下,然后执行命令

pod lib create 私有库名称
pod lib create ZMDA_category

然后根据提示操作,结束后自动打开。这个命令是自动给你生成了一个模板,他会询问你一些问题。然后工程会自动打开具体见这篇博客

修改podspec文件

修改podspec,home,source,description,depency 这是我的设置

#
# Be sure to run `pod lib lint ZMDA_section.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'ZMDA_section'
  s.version          = '0.2.0'
  s.summary          = 'A  simple of ZMDA_section.'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://dev.tencent.com/u/Jingya_lu/p/ZMDA_section'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'cherishandjay' => 'you@1509028992@qq.com' }
  s.source           = { :git => 'https://git.dev.tencent.com/Jingya_lu/ZMDA_section.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'ZMDA_section/Classes/**/*'
  
  # s.resource_bundles = {
  #   'ZMDA_section' => ['ZMDA_section/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  s.dependency 'HandyFrame'
  s.dependency 'ZMDB_category'  
end

如果依赖其他库的话, podfile里也要写上,如下

use_frameworks!


platform :ios, '8.0'

source 'https://github.com/CocoaPods/Specs.git'
source 'https://git.dev.tencent.com/Jingya_lu/ZMDMain.git'#指定私有库的repo地址。

target 'ZMDA_section_Example' do
  pod 'HandyFrame'
  pod 'ZMDB_category'
   pod 'ZMDA_section', :path => '../'#本地路径

  target 'ZMDA_section_Tests' do
    inherit! :search_paths

    
  end
end

其实大部分都不用改,只有版本号等一些信息需要更新。

编译exapmple

cd到example文件夹,执行pod install之后,把demo编译通过。

验证podspec文件。

  • pod lib lint 验证本地的。

  • //推送到远程仓库之后才能pod spec lint 验证通过。

bogon:ZMDA_category jeremy$ pod spec lint 验证
pod lib lint --allow-warnings//忽略警告

关联远程仓库

先在github上新建一个仓库,然后copy仓库地址,当然podspec中的source也要修改。接着执行下边的命令,进行

  • 远程仓库的关联。
  • 打tag。
$ git add .
$ git commit -s -m "Initial Commit of Library"
$ git remote add origin git@coding.net:wtlucky/podTestLibrary.git           #添加远端仓库
git pull origin master --allow-unrelated-histories 强制合并,会有冲突
解决冲突 然后git add .
bogon:ZMDA_category jeremy$ git commit -m "conflict"

$ git push origin master     #提交到远端仓库
//每次发版本都要打一个tag
$ git tag -m "first release" "0.1.0"
$ git push --tags     #推送tag到远端仓库

注册cocoapod账号

pod trunk register 1509028992@qq.com [ljy]

注册后邮箱会收到邮件,有可能在垃圾邮件里,验证后,执行 pod trunk me 会看到你的基本信息,

把库发布到cocoapod

pod trunk push LHAlertController.podspec --allow-warnings

成功提示

如果你不想把代码传到github上,你可以在自己公司的git服务器建一个私有的pod repo,然后把库push进去。 把私有库添加到我们的pod repo里

pod repo push ZMDMain ZMDA_category.podspec

查看本地spec直接进入这个文件夹目录
~/.cocoapods/repos

7.创建私有pod repo

首先github或者其他类似的代码托管平台建一个项目,然后执行
bogon:ZMDA_category jeremy$ pod repo add ZMDMain https://git.dev.tencent.com/Jingya_lu/ZMDMain.git

8.遇到的问题

https://www.jianshu.com/p/d1b6391e3fca
pod lib lint --sources=私有仓库别名1,私有仓库别名1,master --verbose --allow-warnings
其中 master 是 cocoapods 公共的 https://www.jianshu.com/p/d1b6391e3fca

9.私有库第二次修改 平时修改代码,你只cmmit就可以,如果你要发一个版本给其他人用的话,需要打一个tag。然后重新验证pod spec,然后push到pod repo。 //ZMD_acategory建好了--》CTMedia的分类。 www.jianshu.com/p/b1c6d070c… 给section——a添加target-a文件,添加后,提交git,然后建tag,podspec

参考链接:www.jianshu.com/p/b1c6d070c…

www.jianshu.com/p/096208e1f…

京东组建化 www.infoq.cn/article/jd-…

git 合并分支出现 Please enter a commit message to explain why this merge is necessary;# especiallyit