如何制作自己的ios cocoapods框架

Hello guys!

大家好!

So you wanted to create your framework (no matter, one little custom view or the entire system to rule all your projects) and ran into the issue: you don’t know what to do. That’s OK.

因此,您想要创建框架(无论是一个小的自定义视图还是整个系统来管理所有项目),然后遇到问题:您不知道该怎么做。 没关系。

Many topics about that are outdated and official CocoaPods documentation is skinny. I’m sure, this little guide will be outdated in the close future. But while it’s not, I’m here to help you with this easy deal.

有关此主题的许多主题已过时,并且CocoaPods官方文档不完整。 我敢肯定,这个小指南将在不久的将来过时。 但是,虽然不是,但是我在这里可以帮助您达成这一简单协议。

Let’s go, make something!

走吧,做点什么!

制备 (Preparation)

What do you need to create your own framework?

您需要什么来创建自己的框架?

  1. Latest Xcode.

    最新的Xcode

    For me, it’s the 12.0 version. You can always find the latest version

    对我来说,它是12.0版本。 您总是可以找到最新版本

    here.

    在这里

  2. Latest CocoaPods.

    最新的CocoaPods

    For me, it’s the 1.9.3 version. You can always install the latest version via a command in Terminal:

    对我来说,它是1.9.3版本。 您始终可以通过终端中的命令安装最新版本:

    sudo gem install cocoapods

    sudo gem install cocoapods

  3. Github account.

    Github帐号

项目创建 (Project creating)

So you have all that you need, go next. Create your Xcode project. It’s not necessary, but I advise you to choose the “Framework” type for your project.

这样您便拥有了所有所需的内容,请继续。 创建您的Xcode项目。 不必要,但我建议您为项目选择“框架”类型。

Important! Choose the unique name for your framework. Check here for existed CocoaPods framework names. Try to not forget this, because you will receive related errors really late.

重要! 为您的框架选择唯一的名称。 在此处检查现有的CocoaPods框架名称。 尽量不要忘记这一点,因为您真的会迟到收到相关错误。

Important! Choose a license, because it’s a requirement for the CocoaPods framework. (MIT is the default choice)

重要! 选择一个许可证,因为这是CocoaPods框架的要求。 (MIT是默认选择)

Image for post

Description, .gitignore, and README are optional, but the last one is important to describe your framework on cocoapods.org.

description,.gitignore和README是可选的,但是最后一个对于在cocoapods.org上描述您的框架很重要。

Then, clone your repository on your device with command in Terminal and create your project:

然后,使用Terminal中的命令在您的设备上克隆存储库并创建您的项目:

  1. Reach your project directory.

    到达您的项目目录

    For me it’s

    对我来说

    Documents/Projects/Medium: cd ~/Documents/Projects/Medium

    文件/专案/媒体cd ~/Documents/Projects/Medium

  2. And then just clone your repository:

    然后克隆您的存储库

    And then just clone your repository:git clone https://github.com/<your_github_username>/<repo_name>.git(e.g., git clone https://github.com/krotoff/KrotoffMediumProject.git)

    然后只需克隆您的存储库即可git clone https://github.com/<your_github_username>/<repo_name>.git (例如, git clone https://github.com/krotoff/KrotoffMediumProject.git )

  3. Create an Xcode project.

    创建一个Xcode项目

    Open your Xcode app, choose

    打开您的Xcode应用,选择

    File -> New -> Project, select type “Framework” and select the directory you created with git clone.

    File-> New-> Project ,选择类型“ Framework”,然后选择您使用git clone创建的目录。

Image for post

Great! You created your project. Now you can fill it with your extensions/classes/protocols and etc.

大! 您创建了项目。 现在,您可以使用扩展名/类/协议等填充它。

Important! Don’t forget to clarify all entities as public or open to make them available to use with import.

重要! 不要忘了澄清所有实体是public还是open 使它们可与import一起使用。

Also, don’t forget a simple rule: lower supported iOS version you choose earlier your framework will be unsupported (e.g. with Xcode 12 release we lost iOS 8 support in newer projects). So if you choose minimal available iOS version to support, be ready to update your framework with the new Xcode release.

另外,不要忘记一个简单的规则:您选择的较低的受支持的iOS版本将不支持您的框架(例如,在Xcode 12版本中,我们在较新的项目中失去了对iOS 8的支持)。 因此,如果您选择支持的最低可用iOS版本,请准备好使用新的Xcode版本更新框架。

Done with it? Nice. Go next.

完成了吗? 真好下一个

创建一个.podspec文件(Creating a .podspec file)

The most terrible part of our journey. A specification has a huge set of hidden issues. So we’ll try to avoid them all.

我们旅程中最可怕的部分。 规范有很多隐藏的问题。 因此,我们将尽量避免所有这些。

Let’s create this monster with a few steps:

让我们用几个步骤创建这个怪物:

  1. Create a .podspec file.

    创建一个.podspec文件

    Be sure, that you are in your project directory. If not, reach it with the

    确保您在项目目录中。 如果没有,请通过

    cd command (e.g. cd ~/Documents/Projects/Medium/KrotoffMediumProject)

    cd命令(例如cd ~/Documents/Projects/Medium/KrotoffMediumProject )

    Create a file with

    使用创建文件

    touch command: touch KrotoffMediumProject.podspec

    touch命令: touch KrotoffMediumProject.podspec

  2. Fill it.

    填满它

    Open your .podspec file with a text editor.

    使用文本编辑器打开.podspec文件。

    Here is the minimal

    这是最小的

    required set of filled fields:

    必填字段集:

The last 3 fields are not required, but still important. source_files helps you to clarify paths to required files, swift_versions and ios.deployment_target help you to avoid warnings (or even errors) with linting your framework.

最后3个字段不是必需的,但仍然很重要。 source_files可帮助您弄清所需文件的路径, swift_versionsios.deployment_target帮助您避免在ios.deployment_target框架时出现警告(甚至错误)。

But let’s figure out all these unfamiliar lines:

但是,让我们找出所有这些不熟悉的行:

  1. name — it’s quite obvious, right? Unique name of your framework.

    名称-很明显,对吧? 框架的唯一名称

  2. version — version of your framework. Must be the same with version of your project.

    版本-框架的版本。 必须与项目的版本相同

  3. authors — list of project authors. Could be listed with commas.

    作者-项目作者列表。 可以用逗号列出。

  4. license — license type and file. Quite obvious as well. Just must be the same with your license.

    license-许可证类型文件。 也很明显。 只是必须与您的许可证相同。

  5. homepage — web page of your project. Mostly, it’s a github page, but if you have a landing page or something like this, you can write a link on it.

    主页-项目的网页。 通常,这是一个github页面,但是如果您有登录页面或类似的页面,则可以在其上写一个链接。

  6. source — source for your repository. Clarify link, branch, and tag. In my case, I used a version from specification, transformed to string. If you do it another way, just clarify it in quotes as any other fields.

    -您的存储库的源。 澄清链接分支标签。 就我而言,我使用了规范中的一个版本,然后转换为字符串。 如果您以其他方式执行此操作,则只需在引号中将其与其他任何字段一样加以澄清。

  7. summaryshort summary of your framework. Will be used on cocoapods.org.

    摘要—框架的简短摘要。 将用于cocoapods.org

  8. source_files — list of files you need to use from your framework. By default, all root directory files are source files. But you can clarify paths with your needs. **— mask to use any recursive directories. *— mask to use any file. *.swift— mask to use any .swift file.

    source_files-您需要在框架中使用的文件列表。 默认情况下,所有根目录文件均为源文件。 但是您可以根据自己的需要弄清路径。 ** -使用任何递归目录的掩码。 * —使用任何文件的掩码。 *.swift —使用任何.swift文件的掩码。

  9. swift_versions — list of swift versions that used in your framework. Could be a few separated with commas.

    swift_versions-框架使用的swift版本的列表。 可以用逗号隔开。

  10. ios.deployment_targetminimal iOS version for your framework.

    ios.deployment_target-框架的最低iOS版本

You can read more about a specification here. In each section, authors described related fields.

您可以在此处阅读有关规范的更多信息。 在每个部分中,作者都​​描述了相关领域。

Troubles. Most popular of them:

麻烦。 最受欢迎的:

  • If you don’t clarify any of required fields — an error.

    如果您不确定任何必填字段-错误。
  • If you don’t clarify source_files, but you have some required files not in root directory — you just can’t use these files with an import.

    如果您没有弄清source_files ,但是您在根目录下没有一些必需的文件,那么您就不能在导入时使用这些文件。

  • If you don’t clarify swift_versions, be ready to get an error about unknown types/methods and etc.

    如果您不确定swift_versions ,请准备好获取有关未知类型/方法等的错误。

  • The name must be unique and the same with your project name. If not — an error.

    名称必须唯一,并且与您的项目名称相同。 如果不是,则为错误。

  • The version must be the same with you project version. If not — an error.

    版本必须与您的项目版本相同。 如果不是,则为错误。

  • The source must be valid. Check it several times. Really, it’s very often point to make a mistake.

    来源必须有效。 检查几次。 确实,经常会犯一个错误。

  • The summary must be shorter than the description. If not — an error.

    摘要必须短于描述。 如果不是,则为错误。

  • Check here source_files syntax. There is a bunch of hints.

    在此处检查source_files语法。 有很多提示。

So, you are ready to publish? Great! Go next.

那么,您准备发布了吗? 大! 下一个

使用Git(Work with Git)

The next steps are quite simple, but it has some moments, so we will discuss it.

后续步骤非常简单,但是有一些时间,因此我们将对其进行讨论。

  1. Commit and push your code.

    提交并推送您的代码

    Add all project files to stage:

    将所有项目文件添加到阶段:

    git add .Commit them with your message: git commit -m "your message"Push your changes: git push -u origin master

    git add . 用您的消息向他们提交: git commit -m "your message" 推送您的更改: git push -u origin master

  2. Make a tag. It’s a good practice to make a tag the same with your framework version.

    做一个标签。 最好使标记与您的框架版本相同。

    Make a tag:

    制作标签:

    git tag 0.1.0And push it: git push --tags

    git tag 0.1.0 并推送它: git push --tags

  3. You can create a release with your tag, but it is not required. It would be just useful to observe your releases in perspective.

    可以使用标签创建发布,但这不是必需的。 透视您的发布将很有用。

播客发布 (Pod publishing)

Okay, we went through the common actions of iOS developers. So now the special final.

好的,我们经历了iOS开发人员的常见操作。 所以现在特别决赛。

First, you need to check your pod withpod lib lint If you did everything correctly, you’ll see cherished text: passed validation.

首先,您需要使用pod lib lint检查您的pod如果正确执行所有操作,您将看到珍贵的文本:通过了验证

If not, try to fix issues. If it’s not clear, try to add the flag --verbose to the previous command. It will give you detailed info about linting.

如果不是,请尝试解决问题。 如果不清楚,请尝试在前面的命令中添加--verbose标志。 它将为您提供有关棉绒的详细信息。

Passed? Great! Next step — register yourself in CocoaPods via commandpod trunk register your_emailYou will receive an email with a confirmation link. Just click on it and see the CocoaPods confirmation page.

通过了吗大! 下一步-通过命令pod trunk register your_email在CocoaPods中进行pod trunk register your_email您将收到一封包含确认链接的电子邮件。 只需单击它,然后查看CocoaPods确认页面。

Got it? Go next, push your spec via commandpod trunk push FrameworkName.podspec (e.g. pod trunk push KrotoffMediumProject.podspec)If everything is OK, you will get something like that in your console:

得到它了? 接下来,通过命令pod trunk push FrameworkName.podspec推送您的规范(例如pod trunk push KrotoffMediumProject.podspec )。如果一切正常,您将在控制台中看到类似的内容:

Image for post

Important! Sometimes CocoaPods frameworks have a glitch with their local repo. If you want to use your framework on the same device, where the pod was created, you need to clear the trunk repo via command:pod repo remove trunkAnd, as an option, you will need to update thelocal repo with installation:pod install --repo-update

重要! 有时,CocoaPods框架在其本地存储库中会出现故障。 如果要在创建Pod的同一设备上使用框架,则需要通过以下命令清除中继仓库: pod repo remove trunk而且,作为一项选择,您将需要通过安装来更新本地仓库: pod install --repo-update

就这样! (That’s all!)

Quite simple, isn’t it? But without concrete information, it could take a lot of time. So I just hoped to save your time. Here is the example project:

很简单,不是吗? 但是,如果没有具体信息,可能会花费很多时间。 所以我只是希望节省您的时间。 这是示例项目:

Easy coding and fewer bugs to you, see ya!

轻松编写代码,减少错误,请见!

翻译自: https://medium.com/swlh/how-to-make-your-own-ios-cocoapods-framework-99605a9f954c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值