前两篇文章介绍了如何创建一个公有的CocoaPods库,并且更新版本。但有时公司的一些核心代码不想公开出去,就可以创建一个本地私有的CocoaPods库。直接进入主题,创建步骤如下:

1.首先创建一个DemoProject工程,用于测试后面生成的私有库

iOS 私有方法私有对象 ios私有库创建_私有库

2.使用命令创建私有库,并且按照提示回答几个问题:

pod lib create LTFramework
  • 私有库使用什么语言?
  • 私有库中是否需要包含一个demo工程?
  • 私有库是否需要包含一个测试框架?
  • 私有库的类前缀是什么?

iOS 私有方法私有对象 ios私有库创建_私有库_02

3.可以在DemoProject同级目录中看到,私有库工程已经创建好了,并且会自动打开

iOS 私有方法私有对象 ios私有库创建_CocoaPods_03

4.在私有库中加入一个分类文件,重新执行命令

pod install

iOS 私有方法私有对象 ios私有库创建_iOS 私有方法私有对象_04

5.在DemoProject中添加pod支持,命令如下:

pod init // 1.初始化pod
touch podfile // 2.生成podfile文件
open podfile // 3.打开podfile文件
在podfile文件中添加”LTFramework”私有库路径,再重新执行命令安装私有库:
pod install

iOS 私有方法私有对象 ios私有库创建_CocoaPods_05

6.在DemoProject中成功依赖私有库

iOS 私有方法私有对象 ios私有库创建_CocoaPods_06

7.使用私有库中的方法

iOS 私有方法私有对象 ios私有库创建_CocoaPods_07

8.在pod lib create时可能会出现如下的错误:

JerryMBP:~ Jerry.Yao$ pod lib create LTFramework  
Cloning `https://github.com/CocoaPods/pod-template.git` into `LTFramework`.  
Configuring LTFramework template.  
/Users/Jerry.Yao/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:120:in `require': cannot load such file -- colored2 (LoadError)  
    from /Users/Jerry.Yao/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:120:in `require'  
    from /Users/Jerry.Yao/Desktop/Test/LTFramework/setup/TemplateConfigurator.rb:2:in `<top (required)>'  
    from ./configure:5:in `require_relative'  
    from ./configure:5:in `block in <main>'  
    from ./configure:4:in `each'  
    from ./configure:4:in `<main>'  

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.  
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
解决办法是输入如下两条命令行:
sudo gem install colored2
sudo gem update --system