美文网首页ToolsIOS理论知识程序员
组件化最新CocoaPods私有库制作

组件化最新CocoaPods私有库制作

作者: 秦明Qinmin | 来源:发表于2017-03-22 14:31 被阅读601次

前言

之前项目中的一些组件需要拆分开来,因为这些组件在不同的APP中都需要使用(比如:图片上传模块,工具类库等)。因此为了统一管理且不暴露核心的代码,我们决定借助CocoaPods建立私有仓库进行统一管理,也就是所说的组件化。组件化好处是分工更加明确,提高开发效率,复用性更好,能迅速的组成更多的APP。

制作私有库

1、创建私有仓库

这里我在coding上建立了一个私有仓库

https://git.coding.net/kitimy/QMiOSRepo.git

2、添加私有仓库到本地pod库

pod repo add QMiOSRepo https://git.coding.net/kitimy/QMiOSRepo.git

成功之后,我们可以查看一下:
open ~/.cocoapods/repos


私有仓库

在这里多了一个我们的私有库QMiOSRepo

3、创建工程QMKit的git私有仓库

https://git.coding.net/kitimy/QMKit.git

4、在本地建立pod工程

pod lib create QMKit

在创建的过程中会有一系列的询问:

What is your email?
What language do you want to use?? [ Swift / ObjC ]
Would you like to include a demo application with your library? [ Yes / No ]
Which testing frameworks will you use? [ Specta / Kiwi / None ]
Would you like to do view based testing? [ Yes / No ]
What is your class prefix?

完成一系列询问后,可以看到在当前目录多出了QMKit文件夹


QMKit.png

5、编辑podspec文件
podspec 描述自己组件工程的代码目录和资源目录在哪,还有自己组件工程所依赖其他框架,到时候就会根据podspec的指引去引入自己的仓库代码。这里我们在QMKit目录可以看见一个名为QMKit.podspec的文件。

#
# Be sure to run `pod lib lint QMKit.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
#
 // s代表索引库
Pod::Spec.new do |s|
  // 设置名称
  s.name             = 'QMKit'
  // 设置版本号
  s.version          = '0.1.0'
 // 设置摘要
  s.summary          = 'A short description of QMKit.'

# 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://git.coding.net/kitimy/QMKit.git'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  
  // 设置许可证,不能乱填,必须是有这样的协议
 s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'QinminiOS' => '544652424@qq.com' }

  // QMKit的git私有仓库地址
  s.source           = { :git => 'https://git.coding.net/kitimy/QMKit.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'
  // 设置 源文件路径
  s.source_files = 'QMKit/Classes/**/*'
  
  # s.resource_bundles = {
  #   'QMKit' => ['QMKit/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

6、提交代码,并打tag

git add .
git commit -m "first commit"
git remote add origin https://git.coding.net/kitimy/QMKit.git
git push -u origin master
git tag 0.0.1
git push --tags

7、检测pod库是否可用

pod lib lint
如果有警告可以使用命令忽略:
pod lib lint --allow-warnings
  • 如果遇到 [!] An unexpected version directory App.lproj was encountered for the etc... 这个类似的错误说明cocoapods版本比较低用gem install cocoapods --pre更新重试

  • 如果遇到其他问题我们可以用pod lib lint --verbose查看编译错误并进行排查。

8、向QMiOSRepo提交QMKit.podspec

进入QMKit目录,执行以下命令

pod repo push QMiOSRepo QMKit.podspec

9、检测一下pod库是否可用

新建QMTest项目,编写Podfile文件,指定先查找我们的仓库QMiOSRepo.git

source 'https://git.coding.net/kitimy/QMiOSRepo.git'

platform:ios, '8.0'
target 'QMTest' do
pod 'QMKit', '~> 0.1.0'
end
  • 如果出现这个错误: [!] An unexpected version directory Assets was encountered for the
    ~/.cocoapods/repos/QMiOSRepo Pod in the QMiOSRepo repository. 可能是私有仓库地址不对,也可能是向QMiOSRepo提交QMKit.podspec失败。

资源处理

如果组件中使用了资源,一定要把组件生成framework,不能生成静态库,否则资源拿不到。

  • 在使用pod lib create创建的组件工程,会创建Assets文件夹,把图片、资源放这个目录。
QMKit.png
  • 然后podspec指定资源文件路径s.resource_bundles
   s.resource_bundles = {
     'QMKit' => ['QMKit/Assets/*.png']
  }
  • Podfile文件中添加描述use_frameworks!,作用是生成frameworks库。
use_frameworks!

source 'https://git.coding.net/kitimy/QMiOSRepo.git'

platform:ios, '8.0'
target 'QMTest' do
pod 'QMKit', '~> 0.1.1'
end
  • 加载图片资源。资源最终会拷贝到QMKit.framework中的QMKit.bundlke中。
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"QMKit.bundle/1.png" ofType:nil];
 _imageView.image = [UIImage imageWithContentsOfFile:path];
资源在framework中的位置 framework在app中的位置

参考文章

http://guides.cocoapods.org/making/private-cocoapods.html

相关文章

网友评论

  • 十一岁的加重:好东西 啊,有时间实践一吧
  • 516a4e0042f8:请问这个问题怎么解决 [!] An unexpected version directory Assets was encountered for the
    ~/.cocoapods/repos/QMiOSRepo Pod in the QMiOSRepo repository
    podspec文件我写的也是和你一样的了
    516a4e0042f8:我在Podfile里面用了pod 'CocoapodsTest2', :git => 'https://git.coding.net/moekyo/CocoapodsTest2.git,上面的问题倒是不出现可以正常安装了,但是安装完成打开项目,在Pods目录下的CocoapodsTest2文件夹是空的,测试的私有库文件都不见了。如果看到,有空的话请帮忙解答一下,谢谢

本文标题:组件化最新CocoaPods私有库制作

本文链接:https://www.haomeiwen.com/subject/lzeonttx.html