iOS 开发之创建自己的 CocoaPods 库

前言

在 iOS 开发中,我们经常会使用到一些第三方库,如 AFNetworking、SDWebImage 等,一般使用 CocoaPods 来统一管理。那么如何把自己写的类库上传到 CocoaPods,使之可以像 AFNetworking 一样使用呢?即项目的模块化、组件化。

详细步骤

1.在本地创建 pod 库
  • 打开终端,建一个路径,我们以桌面为例,在桌面上创建一个名为 SWHelloWorld 的 pod 库
guosongwei@guosongweideiMac ~ % cd desktop
guosongwei@guosongweideiMac desktop % pod lib create SWHelloWorld
  • 回车之后,终端会询问你几个哲学的问题,按需求填写即可
#选择开发平台
What platform do you want to use?? [ iOS / macOS ]
 > iOS

#选择编程语言
What language do you want to use?? [ Swift / ObjC ]
 > ObjC

#在你的项目中是否创建一个 demo 工程,为了方便测试,我选择了 Yes
Would you like to include a demo application with your library? [ Yes / No ]
 > Yes  

#测试框架选择哪一个
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?
 > SW
安装 pod 成功.png

到这里 pod 库就创建完成了,它会自己打开刚才创建的 pod 库。目录结构如下图:


SWHelloWorld.png
2.添加库文件
  • 我们创建一个简单的类 SWTestManager,声明一个打印方法,以便后面检测是否可用。
#import "SWTestManager.h"

@implementation SWTestManager
- (void)testPrint:(NSString *)text {
    NSLog(@"SWTestManager print == %@",text);
}

@end
  • 将库文件放入 Classes 文件夹里


    SWTestManager.png
  • 在 Example 路径下执行 pod install 命令,看看是否能将刚刚添加的库文件引入到工程中
guosongwei@guosongweideiMac desktop % cd /Users/guosongwei/Desktop/SWHelloWorld/Example 
guosongwei@guosongweideiMac Example % pod install
终端命令执行.png
  • pod install 执行完成后可以在工程中看到添加的文件已经被成功引入


    SWHelloWorld.png
  • 运行测试
    在 SWViewController.m 里面导入 SWTestManager.h 文件,在 viewDidLoad 调用一下方法:
//
//  SWViewController.m
//  SWHelloWorld
//
//  Created by Guo-Sir on 04/08/2021.
//  Copyright (c) 2021 Guo-Sir. All rights reserved.
//

#import "SWViewController.h"
#import "SWTestManager.h"

@interface SWViewController ()

@end

@implementation SWViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[[SWTestManager alloc] init] testPrint:@"真帅!"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

运行一下:


运行.png

看到控制台输出的信息了,证明我们本地运行代码没问题,SWTestManager 可以调用!

3. 编辑 SWHelloWorld.podspec 文件

PS:github、码云…都行,这里以 github 为例

  • 首先在 github 创建一个新的 repository,点击右上角➕,选择 New repository
New repository.png
Create a new repository.png
  • 填写设置好之后,点击 Create repository 按钮,创建完成


    finshed.png
  • 修改 SWHelloWorld.podspec 文件
    右击 SWHelloWorld.podspec 选择打开方式“文本编辑”打开

#
# Be sure to run `pod lib lint SWHelloWorld.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             = 'SWHelloWorld'
  s.version          = '0.1.0' // 版本号必须和 tag 一致
  s.summary          = 'SWHelloWorld.'  // 说明,改一下,默认提交时会报错

# 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/Guo-Sir/SWHelloWorld'  // git 仓库主页
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Guo-Sir' => '[email protected]' }
  s.source           = { :git => 'https://github.com/Guo-Sir/SWHelloWorld.git', :tag => s.version.to_s }  // git 仓库地址
  # s.social_media_url = 'https://twitter.com/'

  s.ios.deployment_target = '9.0'

  s.source_files = 'SWHelloWorld/Classes/**/*'  // 类库文件代码路径
  
  # s.resource_bundles = {
  #   'SWHelloWorld' => ['SWHelloWorld/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end
  • 验证配置是否正确:
    终端进入根目录 SWHelloWorld,执行命令:pod lib lint SWHelloWorld.podspec ,(如果有警告请尝试:pod lib lint SWHelloWorld.podspec --allow-warnings) 提示 SWHelloWorld passed validation. 表示正确。
guosongwei@guosongweideiMac ~ % cd /Users/guosongwei/Desktop/SWHelloWorld 
guosongwei@guosongweideiMac SWHelloWorld % pod lib lint SWHelloWorld.podspec --allow-warnings
pod lib lint SWHelloWorld.podspec.png

至此,配置完成了

4. 项目发布

cd 到项目路径下,执行 git 相关命令,将项目发布到上一步创建的 github 里

guosongwei@guosongweideiMac Example % cd /Users/guosongwei/Desktop/SWHelloWorld 
# 添加远程地址,即上面创建 github 项目的地址
guosongwei@guosongweideiMac SWHelloWorld % git remote add origin https://github.com/Guo-Sir/SWHelloWorld.git
# 添加文件
guosongwei@guosongweideiMac SWHelloWorld % git add .
# 提交本地,并写描述
guosongwei@guosongweideiMac SWHelloWorld % git commit -a -m "第一次提交 版本为 0.1.0" 
# --allow-unrelated-histories
# git pull origin maste 会失败 ,提示:fatal: refusing to merge unrelated histories
# 原因是远程仓库 origin 上的分支 master 和本地分支 master 被 Git 认为是不同的仓库,所以不能直接合并,需要添加 --allow-unrelated-histories
guosongwei@guosongweideiMac SWHelloWorld % git pull origin master --allow-unrelated-histories
# 推送到 github 的 SWHelloWorld 项目的 master 分支上
guosongwei@guosongweideiMac SWHelloWorld % git pull origin master
# 提交版本号
guosongwei@guosongweideiMac SWHelloWorld % git tag 0.1.0
# push 到远程分支
guosongwei@guosongweideiMac SWHelloWorld % git push origin 0.1.0
5.注册 CocoaPods Trunk

在 CocoaPods Trunk 中注册,用邮箱注册,注册成功会有邮件返回。

guosongwei@guosongweideiMac SWHelloWorld % pod trunk register [email protected]

等注册的邮箱收到 CocoaPods 的确认邮件。点击邮件中的验证链接:


邮件.png

页面跳转,提示 Ace, You're set up. 然后才可以进行下一步(如果是升级也需要执行这一步!)。


Ace, You're set up.png
6.发布自己的类库

终端进入根目录 SWHelloWorld,执行命令:pod trunk push SWHelloWorld.podspec --verbose
(如果有警告请尝试:pod trunk push SWHelloWorld.podspec --verbose --allow-warnings)

guosongwei@guosongweideiMac SWHelloWorld % pod trunk push SWHelloWorld.podspec --verbose --allow-warnings

执行完上述命令之后可以去 github 上查看是否已经推送上去了 https://cocoapods.org/pods/SWHelloWorld

Guo-Sir/ SWHelloWorld

这里我们可以看到 github 上已经推上去了

7.尝试使用
  • 我们新建一个项目并引用我们的 pod 库,podfile 里添加如下:
pod 'SWHelloWorld',:git =>"https://github.com/Guo-Sir/SWHelloWorld.git"
  • 执行pod命令:pod install
    我们可以看到工程里已经把自己的类库 pod 下来了,完美!
    以后就可以在任何工程直接使用自己发布的类库了


    success.png

好了,以上就是创建私有库的教程,希望能对大家有所帮助。如有错误,欢迎批评指正!

最后附上官方教程网站:https://guides.cocoapods.org/making/making-a-cocoapod.html

喜欢的话记得给我个 ☆ 喔 O(∩_∩)O~

你可能感兴趣的:(iOS 开发之创建自己的 CocoaPods 库)