摘要
iOS 中,我们经常使用pod ,同时也可以使用组件化开发的方式对代码进行物理隔离,让工程底层彻底解耦,当然自己的工程代码肯定不能上传到公共库上,所以本篇文章描述如何搭建一个自己的私有库
准备工作:
- 创建远程私有库的索引库
https://gitee.com/L63C/l63Specs.git
git@gitee.com:L63C/l63Specs.git
开始创建
- 创建远程私有库
https://gitee.com/L63C/owt_ios.git
git@gitee.com:L63C/owt_ios.git
- 将远程库关联到本地
pod repo add l63Specs https://gitee.com/L63C/l63Specs.git
- 可以在’cd ~/.cocoapods/repos ’ 重查看结果
- 创建本地库
pod lib create <库名>
- 根据提示输入信息,然后就可以生成一个工程并自动打开
4.将对应的资源放入项目根目录下同名的一个文件中 - Classess 用来存放类(默认生成)
- Assets 用来存放图片等资源
- Lib 用来存放静态库(需要新建,如果有需要)配置的时候和 s.vendored_libraries相互对应
- Frameworks 用来存放动态库(需要新建,如果需要)配置的时候和 s.vendored_frameworks相互对应
5.将各种资源放入到对于的文件夹中
- 配置
xxx.podspec
文件
- s.summary 项目摘要(不该会报错和警告)
- s.description 项目描述 (不该会报错和警告)
- s.homepage 个人主页地址
- s.source 库的远程仓库地址
- s.source_files 文件资源
eg: s.source_files = 'OWT_iOS/Classes/**/*'
- s.vendored_frameworks 自己或者第三方的动态库
s.vendored_frameworks = 'OWT_iOS/Frameworks/*.framework'
- s.vendored_libraries 自己或者第三方的静态库
'OWT_iOS/Lib/*.a'
- s.resource_bundles 图片资源
eg: s.resource_bundles = { 'OWT_iOS' => ['OWT_iOS/Assets/*.png'] }
- s.public_header_files 可以暴露出去的头文件
eg: s.public_header_files = 'OWT_iOS/Classes/**/*.h'
- s.frameworks 官方的动态库
eg: s.frameworks = 'UIKit', 'MapKit'
- s.library 官方的静态库
eg: s.library='xml2'
- s.libraries 官方的tdbk库 eg:s.libraries = “c++.1” 注意:需要省去前缀li b
- s.dependency 依赖第三方pod 库
eg: s.dependency 'AFNetworking', '~> 2.3'
- 如果要想看更详细的说明可以创建一个spece文件
pod spec create <文件名>
- 默认配置所有的类都在classes 文件夹下,如果需要分不同的文件结构可以如下陪配置 eg
xxxx 为文件名
s.subspec 'XXXX' do |spim|
spim.source_files = 'SPIM/Classes/SPIM/**/*'
spim.public_header_files = 'SPIM/Classes/SPIM/**/*.h'
spim.dependency 'SPIM/CubeWare'
end
- 本地验证
pod lib lint --verbose --allow-warnings --use-libraries
- 提交代码到远程仓库
1. 创建 git 仓库:
mkdir test
cd test
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/L63C/test.git
git push -u origin master
2. 已有仓库?
cd existing_git_repo
git remote add origin https://gitee.com/L63C/test.git
git push -u origin master
- 打tag 确保tag 和pddspec 中的
s.versioin
一致
git tag xxx
git push --tags
- 远程验证
pod spec lint --verbose --allow-warnings --use-libraries
- 提交到远程索引库
pod repo push l63Specs OWT_iOS.podspec --use-libraries --allow-warnings
- 如果podspec中的s.dependency 依赖了自己的私有库,那么需要指定自己私有库的源,e g.
pod repo push l63Specs OWT_iOS.podspec --sources=https://gitee.com/L63C/l63Specs.git --use-libraries --allow-warnings
12.到此,已经将自己的库打包的自己的私有库中了,可以进行使用了
使用
- 打开自己的工程,在pod 头部指定源:
source 'https://gitee.com/L63C/l63Specs.git'
- pod ‘OWT_iOS’,‘0.1.1’
- 打完收工
将私有库公开放到github
1. 注册pod
pod trunk register lu63chuan@163.com ‘L63C’ --description='regist trunk'
2. 发布
pod trunk push LCTools.podspec
错误记录:
Multiple commands produce '/Users/luchuan/Library/Developer/Xcode/DerivedData/webRTC_iOS-cwwsucqfwhgoxbghiwcmexqtadtw/Build/Products/Debug-iphoneos/webRTC_iOS/webRTC_iOS.framework/Info.plist':
1) Target 'webRTC_iOS' (project 'Pods') has copy command from '/Users/luchuan/Documents/Project/BMC/LIB/webRTC_iOS/webRTC_iOS/Classes/WebRTC.framework/Info.plist' to '/Users/luchuan/Library/Developer/Xcode/DerivedData/webRTC_iOS-cwwsucqfwhgoxbghiwcmexqtadtw/Build/Products/Debug-iphoneos/webRTC_iOS/webRTC_iOS.framework/Info.plist'
2) Target 'webRTC_iOS' (project 'Pods') has process command with output '/Users/luchuan/Library/Developer/Xcode/DerivedData/webRTC_iOS-cwwsucqfwhgoxbghiwcmexqtadtw/Build/Products/Debug-iphoneos/webRTC_iOS/webRTC_iOS.framework/Info.plist'
处理方案:
1.如果是在主工程中,在资源中直接info.plist 的索引
2.如果是在自己搭建的组件库中:就把framwork 中的info.plist本地资源删除,本次提示,我就删除了webRTC中的info.plist
出现新问题:
Domain: com.apple.dt.MobileDeviceErrorDomain
Code: -402653103
--
Could not inspect the application package.
Domain: com.apple.dt.MobileDeviceErrorDomain
Code: -402653103
没有info.plist 组件库允许不起来
The following build commands failed:
Ld /Users/luchuan/Library/Developer/Xcode/DerivedData/App-edspkxbtzjthcvcwgkidyscdbxtg/Build/Intermediates.noindex/App.build/Release-iphonesimulator/App.build/Objects-normal/arm64/Binary/App normal arm64
处理方案:
跳过pod lib 架构认证
1. gem which cocoapods 获取到路径:/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.9.1/lib/cocoapods.rb
2.进入目录:open -a xcode /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.9.1/lib/cocoapods/validator.rb
3. 将以下两行注释(目前在840 行)
command += %w(CODE_SIGN_IDENTITY=- -sdk iphonesimulator)
command += Fourflusher::SimControl.new.destination(:oldest, 'iOS', deployment_target)
并在下面新增:
command += %w(--help)
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/L63C/owt_ios.git'
处理方案
远程仓库没有初始化,也不存在master分支,随便在本地或者远程添加一个文件,然后push 或者 pull 就行
当我用swift 做了一个库的时候,导入了我自己的一个私有库LCTools,总是提示找不到
-> LCTableIndex (0.1.0)
- ERROR | [iOS] unknown: Encountered an unknown error (CocoaPods could not find compatible versions for pod "LCTools":
In Podfile:
LCTableIndex (from `/Users/luchuan/Documents/Project/Pod/LCTableIndex`) was resolved to 0.1.0, which depends on
LCTools (= 0.1.0)
所以我就指定了源:
pod lib lint --verbose --allow-warnings --use-libraries --sources=https://gitee.com/L63C/l63Specs.git
但是我用了一个一个三方库,提示我找不到
CDN: trunk Relative path: CocoaPods-version.yml exists! Returning local because checking is only perfomed in repo update
-> LCTableIndex (0.1.0)
- ERROR | [iOS] unknown: Encountered an unknown error (Unable to find a specification for `Masonry` depended upon by `LCTableIndex`
所以我有把官方库也指定
pod lib lint --verbose --allow-warnings --use-libraries --sources=https://gitee.com/L63C/l63Specs.git,https://github.com/CocoaPods/Specs
现在报错提示静态库的问题
-> LCTableIndex (0.1.0)
- ERROR | [iOS] unknown: Encountered an unknown error (The following Swift pods cannot yet be integrated as static libraries:
The Swift pod `LCTableIndex` depends upon `Masonry`, which does not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set `use_modular_headers!` globally in your Podfile, or specify `:modular_headers => true` for particular dependencies.
最后使用,下面命令验证通过
pod lib lint --verbose --allow-warnings --sources=https://gitee.com/L63C/l63Specs.git,https://github.com/CocoaPods/Specs
- pod repo push StarDogSpecs AWSCore.podspec --allow-warnings 时候,遇到
[!] /usr/bin/git -C /Users/luchuan/.cocoapods/repos/StarDogSpecs pull
Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.
解决方案:
进入到cd ~/.cocoapods 中到找到对应的那个specs 在里面随便建一个文件,然后提交,然后在删除,再提交