cocoaPods 开发打包静态库

Cocoapods 作为 OS X 和iOS开发平台的类库管理工具,已经非常完善和强大。这里我们来演示如何创建使用了 CocoaPods 的静态类库以及打包的过程.

基于 Pod 自动创建

只需要输入 pod 的 lib 命令即可完成初始项目的搭建,下面详细说明具体步骤,以 XCLogStaticDemo 作为项目名演示。
1 cd 到一个空文件夹
2 执行 pod lib create XCLogStaticDemo
期间确认问题有:

What language do you want to use?? [ Swift / ObjC ]  
 > ObjC

Would you like to include a demo application with your library? [ Yes / No ]  
 > YES

Which testing frameworks will you use? [ Specta / Kiwi / None ] 
 > None

Would you like to do view based testing? [ Yes / No ]
 > NO

What is your class prefix? 
 > XC

(3) 打开创建好的 XCLogStaticDemo.podspec 内容修改如下

#
# Be sure to run `pod lib lint XCLogStaticDemo.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 http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'XCLogStaticDemo'
  s.version          = '1.0.0'
  s.summary          = 'A short description of XCLogStaticDemo.'

# 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://github.com/name/XCCocoaPodsStaticDemo'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'yourName' => 'yourEmail' }
  s.source           = { :git => 'https://github.com/name/XCCocoaPodsStaticDemo.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  # 部署的系统要求
  s.ios.deployment_target = '8.0'

  # source core  源码
  s.source_files = 'XCLogStaticDemo/Classes/**/*'

  # resource bundles 资源文件
  # s.resource_bundles = {
  #   'XCLogStaticDemo' => ['XCLogStaticDemo/Assets/*.png']
  # }

  # header 头文件
  # s.public_header_files = 'Pod/Classes/**/*.h'
  # dependency frameworks  依赖库
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

进入Example文件夹, 执行 pod install, 让项目安装依赖并配置更新
(5) 在 XCLogStaticDemo/Classes 文件中添加需要的代码:(对应于XCLogStaticDemo.podspec 的 source_files 目录)
(6) 在 demo 中测试, 打印成功
(7) 提交本地代码库到 github
git add .
git commit -m “install Demo”
git push origin master
如果遇到git错误问题,可以参考refusing to merge unrelated histories , 等上传到github上后,打标签
git tag 1.0.0 (与 XCLogStaticDemo.podspec 的 s.version 对应)
git push origin 1.0.0
(9) 创建账号
pod trunk register 邮箱 联系人 –description=”描述” –verbose
创建成功会收到邮件,进邮箱验证
pod trunk me 可查看trunk信息
(10) 验证类库
pod lib lint —allow-warnings (忽略错误)
这里写图片描述
出现 passed validation 验证成功.
(11) 然后提交到pod服务器网络:出现红框内容表示成功,
pod trunk push —allow-warnings
这里写图片描述

遇到的坑有:

(1) [!] {“name”=>[“is already taken”]}, 表示已经有一个重名的公有库存在,重新创建一个公有库,CocoaPods不允许有重名的公有库存在。
(2) 最后所有的验证都通过了也上传成功了,结果使用pod search仍然搜索不到
解决办法:
1、执行 pod setup
如果最底下会输出setup completed。说明执行pod setup成功。
2、如果pod search操作还是搜索失败,删除~/Library/Caches/CocoaPods目录下的search_index.json文件。

rm ~/Library/Caches/CocoaPods/search_index.json

这里写图片描述

然后执行:

pod search XCLogStaticDemo

这里写图片描述

打包类库

需要使用一个cocoapods的插件cocoapods-packager来完成类库的打包。当然也可以手动编译打包,但是过程会相当繁琐。
安装打包插件
终端执行以下命令

sudo gem install cocoapods-packager

pod搜索
这里写图片描述

打包

命令很简单,执行

pod package XCLogStaticDemo.podspec --force

其中–library指定打包成.a文件,如果不带上将会打包成.framework文件。–force是指强制覆盖。最终的目录结构如下
|____XCLogStaticDemo.podspec
|____XCLogStaticDemo-1.0.0
• | |____ios
• | | |_____XCLogStaticDemo.framework

pod package XCLogStaticDemo.podspec –library –force
或者是:
|____XCLogStaticDemo.podspec
|____XCLogStaticDemo-1.0.0
• | |____ios
• | | |_____libXCLogStaticDemo.a

使用自己打包的静态库, 新建项目,并将打包的 framework 引入, 执行成功
这里写图片描述
这里写图片描述

项目github

参考博客:
1. 使用CocoaPods开发并打包静态库
2. 一步一步教你使用CocoaPods打包静态库
3. 细聊 Cocoapods 与 Xcode 工程配置
4. iOS之使用Cocoapods创建公有仓库踩坑记

原文地址:https://www.cnblogs.com/xiaocai-ios/p/7779740.html