Cocoapods私有库搭建

  1. 创建私有spec repo也就是配置仓库。
  • 在路径~/.cocoapods/repos中我们可以看到master这个文件夹,里面存储的都是我们常用的公有库的配置文件,git路径https://github.com/CocoaPods/Specs.git
  • 同样的,我们的私有库,也需要一个类似master的配置仓库,同理这个库也需要一个git远程端,这个仓库可以是公开的,也可以是私有的,如果是私有的,想要访问使用,肯定是需要权限的。

GitLab上创建一个私有配置仓库,名称就叫做PrivateSpec,然后执行如下命令:

pod repo add PrivateSpec(私有配置仓库名称) 私有配置仓库git地址(ssh地址比较好一些)

执行上面命令之后,~/.cocoapods/repos目录下,就能看到PrivateSpec这个仓库了

2.创建Pod项目工程

  • demo名称暂且取名叫做CCLibTest

执行创建命令:

pod lib create CCLibTest

这时候终端输出信息,会有几个问题,可根据需要进行回答

What platform do you want to use?? [ iOS / macOS ]
 > iOS 
What language do you want to use?? [ Swift / ObjC ]
 > ObjC
Would you like to include a demo application with your library? [ Yes / No ]
 > Yes   YES表示生成工程具有demo
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?
 > CC
 
 *************************************************************************************

以下为执行成功,打开工程的详细输出信息:

Cloning `https://github.com/CocoaPods/pod-template.git` into `CCLibTest`.
Configuring CCLibTest template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: 
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )


What platform do you want to use?? [ iOS / macOS ]
 > iOS

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?
 > CC
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `CCLibTest` from `../`
Downloading dependencies
Installing CCLibTest (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `CCLibTest.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'CCLibTest/Example/CCLibTest.xcworkspace'

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`.

成功之后会得到一个CCLibTest工程,目录如下:

我们可以看到CCLibTest/Classes这个文件夹下有一个.m文件ReplaceMe.m,将我们自己的代码放到这个里面,替换掉ReplaceMe.m这个文件。这里面就是我们私有库的代码。CCLibTest/Assets里面主要放一些资源文件。我加了一个测试代码,如图:

进入Example,执行pod install,我们可以看到这个demo中已经可以使用这个库了,但是这个库仅仅存在于本地。

3.接下来就是CCLibTest提交到git上,比如GitLab,Github,码云等,具体命令简要写下

git init
git add .
git commit -am "init" 
git remote add origin https://github.com/xxx/CCLibTest
git push origin master 
//一定要有标签,不然会有下面的警告
//podspec文件中获取Git版本控制的项目需要tag号,
git tag -m "first" "1.0.0" 
git push --tags

4.配置CCLibTest.podspec文件

这个文件在pod lib create 已经自动创建了。

Pod::Spec.new do |s|
  s.name             = 'CCLibTest'
  s.version          = '1.0.0'
  s.summary          = 'A short description of CCLibTest.'
  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://xxxx.com'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'w_sen' => 'xxxx@xxxx.com' }
  s.source           = { :git => 'https://xxxx.com/xxxx/CCLibTest.git', :tag => s.version.to_s }
  s.ios.deployment_target = '8.0'
  s.source_files = 'CCLibTest/Classes/**/*'
end

查阅了下解释,不太全,其实可以自行搜索,

s.name :pod search 搜索的关键词,注意这里一定要和.podspec的名称一样
s.version :版本号,每一个版本对应一个tag
s.summary : 简介
s.homepage : 项目主页地址
s.license : 许可证
s.author : 作者
s.source : 项目的地址
s.source_files : 需要包含的源文件
s.resources: 资源文件
s.dependency :依赖库
s.ios.deployment_target = '8.0'  : 支持的pod最低版本

s.source_files = 'CCLibTest/Classes/**/*'
*匹配所有文件 **匹配所有子目录

验证podspec,进入CCLibTest目录,执行:

pod lib lint是只从本地验证你的pod能否通过验证
pod spec lint是从本地和远程验证你的pod能否通过验证
我一般都是直接使用pod spec lint去验证pod有没有问题

pod spec lint
或者
pod spec lint --allow-warnings
--allow-warnings是允许warning的存在,也就是说当你在pod spec lint验证podspec的时候,如果不加这句,而你的代码里又一些警告的话,是验证不通过的。而加上这句话的话,有警告也能验证通过。

私有pod验证,我最后使用的方法是:
pod spec lint  --sources='私有仓库repo地址https://xxxxxxxxxx'  --verbose --use-libraries --allow-warnings
验证资源如果有第三方依赖库 需要用如下方法:
pod spec lint --sources='私有仓库repo地址https://xxxxxxxxxx,第三方依赖的共有库地址https://github.com/CocoaPods/Specs' --verbose --use-libraries --allow-warnings

验证之中发生了验证error   ERROR | [iOS] file patterns: The `source_files` pattern did not match any file 在确保配置正确,本地验证通过的情况下,发生的错误 用下面方法解决了:
pod cache clean YOUR_POD_NAME

执行之后最后出现 CCLibTest passed validation.验证通过。

5.CCLibTest.podspec上传到私有配置仓库PrivateSpec

执行命令:

pod repo push [本地Spec Repo名称] [podspec文件路径]

我是在项目CCLibTest目录下执行的操作
pod repo push PodPrivate CCLibTest.podspec --verbose --use-libraries --allow-warnings
这样会出现比较详细的信息,如有错误方便查阅,忽略警告,最后我执行成功了。
打开~/.cocoapods/repos/PodPrivate 你会看到里面的CCLibTest的配置文件信息,当然了远端git上也有。

这时候执行如下命令:

pod search CCLibTest 就可以搜到了

6.工程测试一下

注意使用方法如下:

Podfile文件内容:

source私有spec仓库的git地址,而不是某个pod仓库的地址
source 'xxxxxxxxxxxx.PodPrivate.git'
pod 'CCLibTest' 

注意:如果该仓库没有权限,则会失败

最后执行:

pod install 成功了,可以使用了。

以上的记录,方便自己,也方便大家。如有问题,欢迎一起讨论。

问题记录 关于库中需要依赖一些第三方的静态库 会报错如:The 'xxx' target has transitive dependencies that include static binaries: (xxx),解决办法如下:

//spec文件下加上:
  s.pod_target_xcconfig = {
      'FRAMEWORK_SEARCH_PATHS' => '$(inherited) $(PODS_ROOT)/xxxx',
      'OTHER_LDFLAGS'          => '$(inherited) -undefined dynamic_lookup'
  }
//Podfile加上:
pre_install do |installer|
    # workaround for https://github.com/CocoaPods/CocoaPods/issues/3289 问题在这里有讨论
    Pod::Installer::Xcode::TargetValidator.send(:define_method, :verify_no_static_framework_transitive_dependencies) {}
end

//使用过程中还遇到一个问题,pod引入的库报错:ld: -undefined and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together,目前怀疑是某个第三方依赖库不支持bitcode 于是Podfile加入如下代码:
#bitcode enable
post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ENABLE_BITCODE'] = 'NO'
        end
    end
end
//注:目前只是解决了这个问题。。。

转载于:https://my.oschina.net/snOS/blog/2998642

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值