iOS 创建CocoaPods私有库

CocoaPods

一、简介

为了实现组件化开发,方便每个模块到开发和版本控制,为每个组件创建CocoaPods私有库是目前最有效的方案。
最新最详细的教程请前往iOS CocoaPods私有库的创建和版本更新

二、步骤

  1. 打开终端cd进入到要创建私有库到目录
  2. 在终端上执行pod lib create <私有库名称>来创建库
OneMacBookPro:YDShelfLife admin$ cd /Users/admin/Documents/RemoteLibrary
OneMacBookPro:RemoteLibrary admin$ pod lib create PrivatePod
Cloning `https://github.com/CocoaPods/pod-template.git` into `PrivatePod`.
Configuring PrivatePod template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

2020-03-19 11:23:32.387 defaults[62448:8968663]
The domain/default pair of (org.cocoapods.pod-template, HasRunBefore) does not exist
If this is your first time we recommend running through with the guide:
- https://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and click links to open in a browser. )

Press return to continue.

创建前有几个配置需要进行选择

# 选择使用的平台
What platform do you want to use?? [ iOS / macOS ]
 > iOS
# 选择使用的语言
What language do you want to use?? [ Swift / ObjC ]
 > ObjC
# 库是否包含demo应用
Would you like to include a demo application with your library? [ Yes / No ]
 > Yes
# 选择用来测试的framework
Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > None
# 是否需要基于视图进行测试
Would you like to do view based testing? [ Yes / No ]
 > Yes
# 类名前缀
What is your class prefix?
 > YD
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `PrivatePod` from `../`
Downloading dependencies
Installing FBSnapshotTestCase (2.1.4)
Installing PrivatePod (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `PrivatePod.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'PrivatePod/Example/PrivatePod.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
  1. cd到测试项目Example目录,执行pod install命令,完成
OneMacBookPro:RemoteLibrary admin$ cd /Users/admin/Documents/RemoteLibrary/PrivatePod/Example
OneMacBookPro:Example admin$ pod install
Analyzing dependencies
Fetching podspec for `PrivatePod` from `../`
Downloading dependencies
Using FBSnapshotTestCase (2.1.4)
Using PrivatePod (0.1.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
  1. 打开Example项目的PrivatePod.xcworkspace文件,将源文件目录Classes拖进项目,并新建类目NSString+YDCategory文件
    源文件目录Classe

文件目录'Classes'拖进项目的目的是可以自己在源文件进行修改,也可以自己在项目文件夹下新新建个目录,然后再修改一下PrivatePod.podspec文件中的源文件路径s.source_files,'Classes'目录中.gitkeep文件可能被一起拖进项目,可以删除.gitkeep文件的引用。

新建类目NSString+YDCategory文件

在类目中添加方法printSelf

- (void) printSelf
{
    NSLog(@"YDCategory: %@", self);
}

在控制器中添加代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"PrivatePod";
    [self.title printSelf];
}

运行项目输出结果:

运行结果
  1. 修改PrivatePod.podspec文件
    使用Sublime Text打开
    PrivatePod.podspec文件
Pod::Spec.new do |s|
  s.name             = 'PrivatePod'
  s.version          = '0.1.0'
  s.summary          = 'Test Private Podspec.'

# 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         = '//www.greatytc.com/u/6b426bd2ebfd'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '作者' => '邮箱' }
  s.source           = { :git => 'git地址', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'PrivatePod/Classes/**/*'
  
  # s.resource_bundles = {
  #   'PrivatePod' => ['PrivatePod/Assets/*.png']
  # }

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

编辑完成后保存一下,Example项目重新pod install就可以看到Pod中的源文件有刚新建的类目文件

Pod中的源文件

  1. 在码云上新建对应的私有库PrivatePod


    新建私有库

将生成的git地址在PrivatePod.podspec文件中替换对应的git地址

s.source           = { :git => 'git地址', :tag => s.version.to_s }
  1. 验证Pod配置文件pod lib lint
    验证出错

    验证失败,原因是CocoaPods版本太低
    更新CocoaPods
sudo gem install cocoapods

再次验证通过


验证通过
  1. 将项目推送到远程Git仓库
OneMacBookPro:PrivatePod admin$ git remote add origin https://gitee.com/xxxxxx/PrivatePod.git
# 添加所有文件到缓存
OneMacBookPro:PrivatePod admin$ git add .
# 将缓存内容提交到本地仓库
OneMacBookPro:PrivatePod admin$ git commit -a -m "首次提交 version 0.1.0"
# 将本地仓库推送到远程仓库
OneMacBookPro:PrivatePod admin$ git pull origin master --allow-unrelated-histories

推送到远程仓库时出现了冲突,导致推送失败

warning: no common commits
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From https://gitee.com/heroyoungday/PrivatePod
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
OneMacBookPro:PrivatePod admin$ git push origin master
To https://gitee.com/heroyoungday/PrivatePod.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/heroyoungday/PrivatePod.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

冲突出现在README.md文件,因为前面新建仓库的时候,错误的勾选了使用Readme文件初始化这个仓库,本地创建CocoaPods私有库时也自动生成了一个Readme文件,导致推送时发生冲突。

不要添加readme文件

我使用了sourcetree的代码合并工具,删除了远程仓库的readme文件内容,推送成功了,并打了标签

使用sourceTree推送成功

  1. 创建本地Spec管理库
OneMacBookPro:Desktop admin$ cd /Users/admin/Documents/RemoteLibrary/PrivatePod
OneMacBookPro:PrivatePod admin$ pod repo add PrivatePod https://gitee.com/heroyoungday/PrivatePod.git
Cloning spec repo `PrivatePod` from `https://gitee.com/heroyoungday/PrivatePod.git`
OneMacBookPro:PrivatePod admin$ pod repo push PrivatePod PrivatePod.podspec

Validating spec
 -> PrivatePod (0.1.0)
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'PrivatePod' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
    - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')

Updating the `PrivatePod' repo


Adding the spec to the `PrivatePod' repo

 - [Update] PrivatePod (0.1.0)

Pushing the `PrivatePod' repo
  1. 验证私有库是否发布成功
    新建一个项目PrivatePodDemo,cd进入项目根目录,执行vim Podfile命令,Podfile的内容为
platform :ios,'8.0'
target 'PrivatePodDemo' do
   pod 'PrivatePod',:git => 'https://gitee.com/xxxxxxxx/PrivatePod.git'
end

查看Pod中的源码


查看Pod中的源码

在ViewController文件中添加代码

#import "ViewController.h"
#import "NSString+YDCategory.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"PrivatePodDemo";
    [self.title printSelf];
}
@end

运行结果:


运行结果

参考文章:
IOS创建CocoaPods私有库
iOS创建自己的私有cocoaPods库

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

推荐阅读更多精彩内容