iOS之CocoaPods私有库

559 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第13天,点击查看活动详情

创建远程仓库

Git代码托管平台Gitee为案例

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

image-29.png 创建私有仓库STTPrivateRepo

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

image-31.png

搭建本地组件

使用终端,搭建本地组件

pod lib create STTPrivateRepo

-------------------------
//对模块进行以下配置:

//工程类型
What platform do you want to use?? [ iOS / macOS ]
 > iOS

//开发语言
What language do you want to use?? [ Swift / ObjC ]
 > ObjC

//创建App测试项目
Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

//提供frameworks的测试
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?
 > STT

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

image-32.png

  • homepage:没有要求,但必须可以访问
  • sourceGitee上创建的远程仓库地址 工程下的Classes目录,自动生成ReplaceMe.m文件。可将其删除,将真实代码文件拷贝至该目录下

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

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

image-35.png

测试本地组件

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

platform :ios, '9.0'

target 'LGFramework' do
  # use_frameworks!
  pod 'STTPrivateRepo', :path => '/Users/zang/Zang/Spark/LG/git_temp/STTPrivateRepo'
end

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

image-36.png

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

提交组件到远程代码库

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

#添加文件
git add .
    
#把代码提交到本地
git commit -s -m "首次提交"

#关联仓库
git remote add origin https://gitee.com/zangcrab/STTPrivateRepo.git

#把代码提交到远端仓库
git push origin master

设置tag

#版本号必须和.podspec文件中的版本号一致
git tag -m "初始版本" 0.1.0

#设置tag
git push --tags

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

  • 版本号,二者必须一致 命令执行成功后,在Gitee远程仓库中,可以看到代码提交后的变化

image-38.png

提交podspec索引文件

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

pod lib lint --allow-warnings

-------------------------
 -> STTPrivateRepo (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | xcodebuild:  note: Build preparation complete
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Analyzing workspace
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description

STTPrivateRepo passed validation.

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

#pod repo push 本地索引库 索引文件名
pod repo push STTPrivateRepo STTPrivateRepo.podspec --allow-warnings

-------------------------
Validating spec
 -> STTPrivateRepo (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | xcodebuild:  note: Build preparation complete
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Analyzing workspace
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description

Updating the `STTPrivateRepo' repo


Adding the spec to the `STTPrivateRepo' repo

 - [Update] STTPrivateRepo (0.1.0)

Pushing the `STTPrivateRepo' repo

使用组件

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

platform :ios, '9.0'

target 'LGFramework' do
  # use_frameworks!
  pod 'STTPrivateRepo', :git => 'https://gitee.com/zangcrab/STTPrivateRepo.git'
end

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

image-39.png 成功导入STTPrivateRepo组件

image-40.png

备注

github使用access tokens

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

关联仓库:

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

使用组件:

platform :ios, '9.0'

target 'LGFramework' do
  # use_frameworks!
  pod 'STTPrivateRepo', :git => 'https://ghp_xxxxx@github.com/ZangCrab/STTPrivateDemo.git'
end
remote origin already exists

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

#关联仓库
git remote add origin https://gitee.com/zangcrab/STTPrivateRepo.git

-------------------------
fatal: remote origin already exists

此时需要解除之前的关联

git remote rm origin
其他

Git配置多个SSH-Keygitee.com/help/articl…

Git常用命令:gitee.com/all-about-g…

CocoaPods指南:guides.cocoapods.org/