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' => 'xxx@163.com' }
  s.source           = { :git => 'https://github.com/xxx/MAMonitor.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  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://<your_token>@github.com/<USERNAME>/<REPO>.git

  • <your_token>:换成你自己得到的token
  • <USERNAME>:是你自己github的用户名
  • <REPO>:是你的仓库名称

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

  • 我用的是码云 一、首先我们先在码云上建一个远程私有索引仓库 创建后的页面: 二、创建一个项目 创建后页面: 三、打...
    69a8e4612fc7阅读 848评论 1 1
  • 参考链接:http://blog.csdn.net/qxuewei/article/details/5441211...
    海浪萌物阅读 2,298评论 1 8
  • 一、创建私有的Spec Repo(git仓库1) 二、创建Pod项目工程,并且又可以访问的项目版本控制地址(git...
    一亩三分甜阅读 2,425评论 2 2
  • 私有库的是随着公司在多个项目开展的时候,把一些常用的工具类制作成pod,方便在多个项目中使用,避免了来回拖入代码造...
    zl_xust阅读 517评论 0 1
  • 1、在Git仓库分别创建2个私有库,一个索引库,一个是pod代码库 2、将远程索引库添加到本地 a. 首先进入本地...
    我是一根聪阅读 509评论 0 1