iOS--创建私有库

以创建一个名为 MAMonitor 的库为准。

一、创建两个 git 库

  1. MAMonitor(私有库项目名称:https://github.com/xxx/MAMonitor)

  2. MAMonitorSpec(私有库索引库名称:https://github.com/mayuee/MAMonitorSpec)
    这个库用来保存私有库的 podspec文件,一般起名xxxSpec。这个库不存放代码,而是包名、版本号分门别类的存放所有的有关私有库的配置。

复制仓库 git 地址 执行命令:
pod repo add MAMonitorSpec https://github.com/xxx/MAMonitorSpec
查看是否添加成功:
pod repo list

image.png

二、创建本地私有库

cd xxx/MAMonitor         //自定义要创建的(本地存放)私有库的文件夹
pod lib create MAMonitor      //MAMonitor :私有库项目名称

执行如下,按需选择

 Press return to continue.
1.平台
What platform do you want to use?? [ iOS / macOS ]
 > iOS

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

3. 是否集成Demo为自己的模块库?
Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

4. 是否集成测试框架?
Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > None

5. 是否基于View的做测试?
Would you like to do view based testing? [ Yes / No ]
 > Yes

6. 类的前缀
What is your class prefix?
 > M

执行完毕后创建了一个工程


image.png

将Classes文件夹下面的ReplaceMe.m文件删除掉,替换成自己要上传的私有库的代码,然后更新一下这个工程的pod库

  1. cd到Example文件下
  2. 执行 pod install

修改podspec文件
这里是:MAMonitor项目下的MAMonitorSpec.podspec 文件

#
# Be sure to run `pod lib lint MAMonitor.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'MAMonitor'
  s.version          = '0.1.0'
  s.summary          = 'A short description of MAMonitor.'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/xxx/MAMonitor'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'xxx' => '[email protected]' }
  s.source           = { :git => 'https://github.com/xxx/MAMonitor.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/'

  s.ios.deployment_target = '10.0'

#  “*” 表示匹配所有文件,“**” 表示匹配所有子目录
  s.source_files = 'MAMonitor/Classes/**/*'
  
  # s.resource_bundles = {
  #   'MAMonitor' => ['MAMonitor/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

需要修改的项

  1. 修改版本号
  2. 修改项目的简单概述和详细描述
  3. 修改homepage
    s.homepage = 'https://github.com/xxx/MAMonitor'
    改为
    s.homepage = 'https://github.com/gitxxx/MAMonitor.git'
    注意:s.homepage需要设置刚创建的私有代码仓库的地址, 不是私有索引库的地址!!!
  4. 修改source地址
    s.source = { :git => 'https://github.com/xxx/MAMonitor.git', :tag => s.version.to_s }
    改为
    s.source = { :git => 'https://github.com/gitxxx/MAMonitor.git', :tag => s.version.to_s }
    注意:s.source 需要设置的是私有代码仓库的源地址(选择使用HTTPS地址)!!!
  5. 添加依赖库,这里添加 UIKit
    s.frameworks = 'UIKit'

三、将私有库push到远程仓库

编译通过后,提交代码到私有库的git仓库地址 并打tag

git status -- 查看当前git存了什么文件
git add . -- 将所有文件缓存到待提交文件区域
git commit -m "上传组件" -- 提交文件,写上备注
git remote add origin <私有库git仓库地址(https://github.com/gitxxx/MAMonitor.git)>
git pull origin master // 把远程仓库的文件更新到本地
git push -u origin master -- 将代码推送到远程私有库git仓库的master分支
git tag <版本号(例如git tag 0.1.0)> --这里的版本号必须和podspec里面写的版本号一致)
git push -- tags

四、本地校验

到本地私有库目录下:cd <本地私有库目录下(例如:cd MAMonitor)>
执行验证:pod lib lint --allow-warnings

若组件依赖第三方库,需要将第三方库索引地址写上
pod lib lint --sources="cocoapods库地址,私有库远程地址" --allow-warnings

若组件依赖的第三方库又依赖了其他的库,需要命令如下
pod lib lint --sources="cocoapods库地址,私有库远程地址" --use-libraries --allow-warnings

@localhost MAMonitor % pod lib lint --allow-warnings

 -> MAMonitor (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | url: There was a problem validating the URL https://github.com/mayuee/MAMonitor.git.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
MAMonitor passed validation.

到这里,本地校验通过。

五、远程校验

先创建一个 Release版本,基于前面创建的 tag 0.1.0。


image.png
image.png

最后点击 · Publish·


image.png

远程校验和本地校验类似,只要把lib字段改成spec

pod spec lint --sources="cocoapods库地址,私有库远程地址" --use-libraries --allow-warnings

例如:

pod spec lint --sources="https://github.com/gitxxx/MAMonitor.git" --use-libraries --allow-warnings

如下则表示验证通过了。

 % pod spec lint --sources="https://github.com/xxxx/MAMonitor.git" --use-libraries --allow-warnings
 -> MAMonitor (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  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
Analyzed 1 podspec.
MAMonitor.podspec passed validation.

其间有几次报错

fatal: unable to access 'https://github.com/mayuee/MAMonitor.git/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 

试了几个网络上的方案,最靠谱的方案是 多试几次。。。。

六、提交索引文件到远程索引库

所有验证通过之后,要将spec文件推送到最开始创建的私有库索引库当中

cd 到私有库项目目录(例如:cd MAMonitor)

pod repo push <本地索引库名称> <索引文件名> --verbose --allow-warnings

例如:pod repo push MAMonitorSpec MAMonitor.podspec --verbose --allow-warnings

注意:<本地索引库名称>是 /Users/XXX/.cocoapods/repos下的私有库索引项目名称
<索引文件名> 就是以 podspec 结尾的

推送成功后,在本地索引库中如下图


image.png

添加成功后,索引库 MAMonitorSpec 中会自动出现 MAMonitorMAMonitor 中只包含 MAMonitor.podspec 文件。

image.png

七、验证私有库

  1. pod repo update 先更新一下pod库
    这一步报错:
[!] CDN: trunk Repo update failed - 123 error(s):
CDN: trunk URL couldn't be downloaded: https://cdn.cocoapods.org/AlgoliaSearch.yml Response: Timeout was reached
  1. 解决报错权宜方法

//移除了文件夹 /Users/user/.cocoapods/repos/trunk
pod repo remove trunk
重新安装 //或更新
pod install // or pod update

这样改了之后, 工程的 Podfile 里需要添加一句话
source 'https://github.com/CocoaPods/Specs.git'

  1. 搜索私有库 pod search MAMonitor
-> MAMonitor (0.1.0)
   A short description of MAMonitor.
   pod 'MAMonitor', '~> 0.1.0'
   - Homepage: https://github.com/xxxx/MAMonitor.git
   - Source:   https://github.com/gitxxx/MAMonitor.git
   - Versions: 0.1.0 [MAMonitorSpec repo]
(END)

八、其他项目使用私有库

新建 demo 工程,使用pod安装 MAMonitor

source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/gitxxx/MAMonitor.git'

target 'demo' do
  use_frameworks!

pod 'MAMonitor'

end

执行 pod install(pod update)报错

[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `cocoapods`.
You can try adding it manually in `/Users/mazhibao/.cocoapods/repos` or via `pod repo add`.

这是因为前面移除了 repos目录下的原本默认的 trunk,但是master 没有,后面需要手动添加,解决方案:
1、cd ~/.cocoapods/repos
2、git clone https://github.com/CocoaPods/Specs.git master

移除项目下的 pods(如果已经生成的话) 文件夹,重新 pod install

九、更新

对已有库进行更新,需要创建新的 Release 版本,更改 MAMonitor.podspec 文件版本号,重新执行 pod repo push MAMonitorSpec MAMonitor.podspec --verbose --allow-warnings 即可。

————————————————

其他

  1. 验证 podspec :执行:pod spec lint --allow-warnings
  2. 删除原来的tag 0.0.1
git tag -d 0.0.1  //删除本地 tag
git push origin :refs/tags/0.0.1    //删除远程 tag
  1. 清理pod缓存:pod cache clean MAMonitor

  2. 如果私有库组件库过多,需要分层文件夹显示,则需要使用 subspec

  #spec.source_files     = "Classes", "Classes/**/*" 
  #spec.resources        = "Resources/*.png"

  #spec.source_files  = "Classes", "Classes/**/*.{h,m}"
  #spec.exclude_files = "Classes/Exclude"

  spec.subspec 'GOImagesCarouselView' do |s|
     s.source_files = "Classes/GOImagesCarouselView/**/*"
     s.resources    = "Resources/GOImagesCarouselView/*.png"
  end

???


  1. git push ,在输入 Username 和 Password 之后,Github 返回如下一个错误
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/xxxx/MAMonitor.git/'

查资料,大概意思就是,以前的密码认证从2021年8月13日开始就不能用了,必须使用个人访问令牌(personal access token),即就是把密码替换成token。

背景是近年来,GitHub 客户受益于 GitHub.com 的多项安全增强功能,例如双重身份验证、登录警报、验证设备、防止使用泄露密码和WebAuthn 支持。这些功能使攻击者更难获取在多个网站上重复使用的密码并使用它来尝试访问您的 GitHub 帐户。尽管有这些改进,但由于历史原因,未启用双因素身份验证的客户仍然能够仅使用其 GitHub 用户名和密码继续对 Git 和 API 操作进行身份验证。

从 2021 年 8 月 13 日开始,在对 Git 操作进行身份验证时将不再接受帐户密码,并将要求使用基于令牌的身份验证,例如个人访问令牌(针对开发人员)或 OAuth 或 GitHub 应用程序安装令牌(针对集成商)适用于 GitHub.com 上所有经过身份验证的 Git 操作。您也可以在您喜欢的地方继续使用 SSH 密钥。

与基于密码的身份验证相比,令牌提供了许多安全优势:

  • 唯一 —— 令牌特定于 GitHub,可以按使用或按设备生成
  • 可撤销 —— 令牌可以随时单独撤销,无需更新未受影响的凭据
  • 有限 —— 令牌的范围可以很窄,只允许用例所需的访问权限
  • 随机 —— 令牌不受字典类型或暴力尝试的影响,您需要记住或定期输入的更简单的密码可能是

问题解决

  1. 找到个人Settings页面:
image.png
  1. 找到Developer settings,选择个人访问令牌Personal access tokens,然后选中生成令牌Generate new token


    image.png
  1. 设置token的特性,比如:标题,有效期,token权限等等


    image.png
  1. 点击最下面 生成令牌 Generate token
    image.png

把token复制下来
之后提交代码的时候,在之前输入密码的地方输入这个token就可以了。

也可以 把token直接添加远程仓库链接中,这样就可以避免同一个仓库每次提交代码都要输入token了:

git remote set-url origin https://@github.com//.git

  • :换成你自己得到的token
  • :是你自己github的用户名
  • :是你的仓库名称

还有一点,新创建的repo里面,默认分支是 main 不是 master

你可能感兴趣的:(iOS--创建私有库)