1. 创建远程仓库

Git代码托管平台Gitee为案例

登录Gitee账号,选择新建仓库
image.png

创建私有仓库STTPrivateRepo
image.png

创建成功后,获得远程仓库地址
image.png

2. 搭建本地组件

使用终端,搭建本地组件

  1. pod lib create STTPrivateRepo
  2. -------------------------
  3. //对模块进行以下配置:
  4. //工程类型
  5. What platform do you want to use?? [ iOS / macOS ]
  6. > iOS
  7. //开发语言
  8. What language do you want to use?? [ Swift / ObjC ]
  9. > ObjC
  10. //创建App测试项目
  11. Would you like to include a demo application with your library? [ Yes / No ]
  12. > Yes
  13. //提供frameworks的测试
  14. Which testing frameworks will you use? [ Specta / Kiwi / None ]
  15. > None
  16. //提供测试文件
  17. Would you like to do view based testing? [ Yes / No ]
  18. > No
  19. //设置前缀
  20. What is your class prefix?
  21. > STT

找到.podspec文件,对homepagesource进行配置
image.png

  • homepage:没有要求,但必须可以访问
  • sourceGitee上创建的远程仓库地址

工程下的Classes目录,自动生成ReplaceMe.m文件。可将其删除,将真实代码文件拷贝至该目录下
image.png

使用终端,在工程下的Example目录中,执行pod install
image.png

命令执行成功后,刚才拷贝的代码在项目中生效
image.png

3. 测试本地组件

搭建LGFramework测试项目,在Podfile中写入以下代码:

  1. platform :ios, '9.0'
  2. target 'LGFramework' do
  3. # use_frameworks!
  4. pod 'STTPrivateRepo', :path => '/Users/zang/Zang/Spark/LG/git_temp/STTPrivateRepo'
  5. end

使用终端,在测试项目目录中,执行pod install
image.png

  • 保证本地组件可以正常使用

4. 提交组件到远程代码库

使用终端,在组件目录下,执行以下命令:

  1. #添加文件
  2. git add .
  3. #把代码提交到本地
  4. git commit -s -m "首次提交"
  5. #关联仓库
  6. git remote add origin https://gitee.com/zangcrab/STTPrivateRepo.git
  7. #把代码提交到远端仓库
  8. git push origin master

设置tag

  1. #版本号必须和.podspec文件中的版本号一致
  2. git tag -m "初始版本" 0.1.0
  3. #设置tag
  4. git push --tags

查看.podspec文件中的版本号
image.png

  • 版本号,二者必须一致

命令执行成功后,在Gitee远程仓库中,可以看到代码提交后的变化
image.png

5. 提交podspec索引文件

使用终端,在组件目录下,执行以下命令:

  1. pod lib lint --allow-warnings
  2. -------------------------
  3. -> STTPrivateRepo (0.1.0)
  4. - WARN | summary: The summary is not meaningful.
  5. - NOTE | xcodebuild: note: Using new build system
  6. - NOTE | xcodebuild: note: Building targets in parallel
  7. - NOTE | xcodebuild: note: Using codesigning identity override: -
  8. - NOTE | xcodebuild: note: Build preparation complete
  9. - NOTE | [iOS] xcodebuild: note: Planning build
  10. - NOTE | [iOS] xcodebuild: note: Analyzing workspace
  11. - NOTE | [iOS] xcodebuild: note: Constructing build description
  12. STTPrivateRepo passed validation.

验证通过之后,提交索引文件到远程索引库

  1. #pod repo push 本地索引库 索引文件名
  2. pod repo push STTPrivateRepo STTPrivateRepo.podspec --allow-warnings
  3. -------------------------
  4. Validating spec
  5. -> STTPrivateRepo (0.1.0)
  6. - WARN | summary: The summary is not meaningful.
  7. - NOTE | xcodebuild: note: Using new build system
  8. - NOTE | xcodebuild: note: Building targets in parallel
  9. - NOTE | xcodebuild: note: Using codesigning identity override: -
  10. - NOTE | xcodebuild: note: Build preparation complete
  11. - NOTE | [iOS] xcodebuild: note: Planning build
  12. - NOTE | [iOS] xcodebuild: note: Analyzing workspace
  13. - NOTE | [iOS] xcodebuild: note: Constructing build description
  14. Updating the `STTPrivateRepo' repo
  15. Adding the spec to the `STTPrivateRepo' repo
  16. - [Update] STTPrivateRepo (0.1.0)
  17. Pushing the `STTPrivateRepo' repo

6. 使用组件

延用LGFramework测试项目,在Podfile中写入以下代码:

  1. platform :ios, '9.0'
  2. target 'LGFramework' do
  3. # use_frameworks!
  4. pod 'STTPrivateRepo', :git => 'https://gitee.com/zangcrab/STTPrivateRepo.git'
  5. end

使用终端,在测试项目目录中,执行pod install
image.png

成功导入STTPrivateRepo组件
image.png

7. 备注

7.1 github使用access tokens

在关联仓库和使用组件时,需要使用access tokens

关联仓库:

  1. #关联仓库
  2. #git remote add origin https://[access tokens]@github.com/ZangCrab/STTPrivateRepo.git
  3. git remote add origin https://ghp_xxxxx@github.com/ZangCrab/STTPrivateDemo.git

使用组件:

  1. platform :ios, '9.0'
  2. target 'LGFramework' do
  3. # use_frameworks!
  4. pod 'STTPrivateRepo', :git => 'https://ghp_xxxxx@github.com/ZangCrab/STTPrivateDemo.git'
  5. end

7.2 remote origin already exists

如果本地代码已经关联其他仓库,再次关联时会报错

  1. #关联仓库
  2. git remote add origin https://gitee.com/zangcrab/STTPrivateRepo.git
  3. -------------------------
  4. fatal: remote origin already exists

此时需要解除之前的关联

  1. git remote rm origin

7.3 其他

Git配置多个SSH-Keyhttps://gitee.com/help/articles/4229#article-header0

Git常用命令:https://gitee.com/all-about-git

CocoaPods指南:https://guides.cocoapods.org/