
一、CocoaPods介绍
安装完CocoaPods后,先来查看仓库。
pod repo

其实仓库就在cocoapods隐藏文件夹中,如下:

这个文件夹是当安装了cocoapods后,第一次pod install的时候,cocoapods通过命令 pod setup 来建立的(这个pod setup命令是不需要手动执行的)。不过如果想重建这个官方repos仓库集合的话,可以执行下面命令:
pod repo remove master
pod setup
二、使用CocoadPods创建私有库
1、在Github上创建一个空的仓库,这个仓库是用来存放私有库spec的。当然也可以使用其他代码托管创建仓库。

2、在Github上创建另一个空的仓库,这个仓库用来存放私有库的源码。

3、创建一个本地私有仓库。
pod repo add MxZSpec https://github.com/vincentgemini/MxZSpec.git
注意这个只是创建而已,也就是只是在~/.cocoapods/repos 目录下添加了一个文件夹而已,并没有在其中添加 spec文件。
4、创建spec库索引。
pod lib create MxZFramework

打开spec文件。

s.name:名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称一样,否则报错
s.version:版本号
s.ios.deployment_target:支持的pod最低版本
s.summary: 简介
s.homepage:项目主页地址
s.license:许可证
s.author:作者
s.social_media_url:社交网址,这里我写的微博默认是Twitter,如果你写Twitter的话,你的podspec发布成功后会@你
s.source:项目的地址
s.source_files:需要包含的源文件
s.resources: 资源文件
s.requires_arc: 是否支持ARC
s.dependency:依赖库,不能依赖未发布的库
5、把创建好的MxZFramework推到远端。
git status
git add .
git commit -m 'init'
git remote add origin https://github.com/vincentgemini/MxZFramework.git
git push origin master
git tag '1.0.1'
git push --tags
由于cocoapods的repo是基于git的tag的,所以需要给MxZFramework添加一个tag.注意,这里添加的tag需要和podspec文件中描述的一致,不然以后别人引用的时候,cocoapods就不知道该下载什么代码了。
6、在MxZFramewrok中添加需要的源码库

7、编辑库索引,打开1生成的spec文件,修改后如下:

8、本地测试库索引,由于可以忽略警告,经常使用如下命令。
pod lib lint MxZFramework.podspec --verbose --allow-warnings
如果当前工程只有一个podspec,可以省略podspec,--verbose可以详细打印测试过程。

9、远端测试库索引,在测试前,需要先将本地的源码库推到远端。此处注意tag一定要与podspec中的tag一致。
git status
git add .
git commit -m 'modify'
git push origin master
git tag '1.0.1'
git push --tags
上传成功后,再使用如下命令。
pod spec lint MxZFramework.podspec --allow-warnings

有时本地测试可以通过,但远端测试无法通过,多半是由于远端代码库的文件结构不对,导致无法找到文件。此时需要对照一下索引库和远端代码库的对应关系。
10、在私有仓库中添加库索引。
pod repo push MxZSpec MxZFramework.podspec
由于索引中一些警告未处理,所以如下如下提示:

解决办法:
pod repo push MxZSpec MxZFramework.podspec --allow-warnings


11、检验私有库
pod search MxZFramework
如下提示表示已经创建成功。

三、使用私用库开发
1、创建一个测试工程testTFW,进入到工程目录,初始化Podfile。
pod init
2、修改Podfile。

3、构建pod。
pod install

失败的原因是:pod install只会在master下搜索,所以需要制定仓库。



四、总结
以上仅仅是本人利用cocoapods封装私有库的简单过程,稍后会持续补充一些遇到的新问题。
以下为补充部分
2018.2.24
-
在添加USER_HEADER_SEARCH_PATHS变量时,发现添加后还是找不到相应的文件。解决办法设置绝对路径:
$dir = File.dirname(__FILE__) $dir = $dir + "/MxZFramework/Vendors/FFmpeg-iOS/include/**" s.xcconfig = { "USER_HEADER_SEARCH_PATHS" => $dir}
-
如果私有库中使用了静态库,那么在pod lib lint或者pod spec lint时需要加上--use-libraries,并且后面添加索引的时候也需要使用--use-libraries。
pod spec lint --use-libraries
2018.3.7
使用inhibit_warnings或inhibit_all_warnings去掉第三方Pod中的警告
2018.3.8
简单记录:
#依赖第三方Framework
s.vendored_frameworks = 'Pod/ThirdModule/**/*.framework'
#依赖第三方Lib
s.vendored_libraries = 'Pod/ThirdModule/**/*.a'
#依赖系统Framework
s.frameworks = 'UIKit', 'QuartzCore', 'CoreGraphics', 'Accelerate', 'AssetsLibrary', 'OpenAL', 'OpenGLES'
#依赖系统Lib
s.libraries = 'icucore', 'c++', 'stdc++', 'stdc++.6', 'z', 'bz2', 'iconv', 'resolv', 'sqlite3', 'protobuf'
私有库中依赖私有库
在podspec中正常编写依赖pod关系:
s.dependency 'xxxModule', '1.0.1'
在检测podspec时,需要增加sources:
pod lib lint --use-libraries --verbose --allow-warnings --sources='https://github.com/CocoaPods/Specs.git,http://xxxxx/app/Speaces.git'
预编译头文件:
#方法一
s.prefix_header_contents = '#import <UIKit/UIKit.h>', '#import <Foundation/Foundation.h>'
#方法二
s.prefix_header_contents = <<-EOS
#ifdef __OBJC__
#import "MxZHeader1.h"
#import "MxZHeader2.h"
#endif
EOS
end
#方法三
s.prefix_header_file = 'Classes/include/prefix.pch'
源文件:
s.source_files = 'Classes/**/*.{h,m}'
s.source_files = 'Classes/**/*.{h,m}', 'Others/**/*.{h,m}'
ARC设置:
s.requires_arc = false
s.requires_arc = 'Classes/Arc' //该文件夹下是ARC,其它非ARC
s.requires_arc = ['Classes/*ARC1.m', 'Classes/ARC2.m']
compiler_flags设置:
s.compiler_flags = '-DOS_OBJECT_USE_OBJC=0', '-Wno-format'
资源设置:
s.ios.resource_bundle = { 'Resources' => 'Assets/XXX/Resources/*.png' }
s.resource_bundles = {
'Resources' => ['Assets/XXX/Resources/*.png'],
'Other' => ['Assets/XXX/Other/*.png']
}
另一种资源设置:
s.resource = 'Resources/MxzKit.bundle'
s.resources = ['Images/*.png', 'Sounds/*']
2018.4.9
当在开发一个项目时可能存在如下情况,在App.Worksapce中有App.project、SDK1.project和SDK2.project,其中App、SDK1和SDK2都使用了第三方库,且都想使用Pods进行管理。这时,需要修改Podfile文件。
workspace 'App'
xcodeproj 'App.xcodeproj'
xcodeproj 'Modules/SDK1/SDK1.xcodeproj'
xcodeproj 'Modules/SDK2/SDK2.xcodeproj'
target :App do
platform :ios, '8.0'
pod 'AFNetworking', '3.0.4'
pod 'Masonry', '0.6.4'
pod 'CocoaLumberjack', '3.2.0'
pod 'CocoaAsyncSocket', '7.6.1'
pod 'ReactiveCocoa', '2.5'
xcodeproj 'MxZPlayer.xcodeproj'
end
target :SDK1 do
platform :ios, '8.0'
pod 'Masonry', '0.6.4'
xcodeproj 'Modules/SDK1/SDK1.xcodeproj'
end
target :SDK2 do
platform :ios, '8.0'
pod 'AFNetworking', '3.0.4'
xcodeproj 'Modules/SDK2/SDK2.xcodeproj'
end
2018.5.31
- 如何维护多个版本cocoapods
查看本地cocoapods版本
gem list cocoapods

安装指定版本cocoapods
sudo gem install cocoapods --version x.x.x
查看pod位置
which pod

修改默认cocoapods版本
vim /Users/xxxx/.rvm/gems/ruby-2.4.1/bin/pod
修改version字段:
1 #!/usr/bin/env ruby_executable_hooks
2 #
3 # This file was generated by RubyGems.
4 #
5 # The application 'cocoapods' is installed as part of a gem, and
6 # this file is here to facilitate running it.
7 #
8
9 require 'rubygems'
10
11 version = "1.3.1"
12
13 if ARGV.first
14 str = ARGV.first
15 str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
16 if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
17 version = $1
18 ARGV.shift
19 end
20 end
21
22 if Gem.respond_to?(:activate_bin_path)
23 load Gem.activate_bin_path('cocoapods', 'pod', version)
24 else
25 gem "cocoapods", version
26 load Gem.bin_path("cocoapods", "pod", version)
27 end
2018.8.10
安装cocoapods-packager
gem install cocoapods-packager
2018.8.29
pod install报错:

问题的主要原因是 Rubygems 在 2.7.7 的版本做了 breaking change。
可以降级到2.7.6,使用如下命令:
gem update --system 2.7.6
执行过程中可能会出现如下错误:

sudo gem sources -r xxxxxx
gem sources -a https://gems.ruby-china.com
网友评论