迦太基 oc_如何使用迦太基分发ios框架

迦太基 oc

模块化iOS (Modular iOS)

Great software also has a great installation experience. If the software is too hard to install few will use it.

出色的软件也具有出色的安装经验。 如果该软件太难安装,将很少使用它。

When it comes to distributing iOS frameworks the installation process can vary depending on the form the framework is delivered. The two forms in which you can deliver your framework are:

在分发iOS框架时,安装过程可能会有所不同,具体取决于框架的交付形式。 交付框架的两种形式是:

  1. source code

    源代码
  2. compiled framework

    编译框架

In this post we’ll focus on the first form. That means the source code is readable by the integrator.

在本文中,我们将重点介绍第一种形式。 这意味着源代码可由集成商读取。

The experience that Apple offers out of the box when installing a iOS framework leaves a lot to desire. First integrators need to get a copy of your code. Then they need to add it to their workspace. Finally they need embed the framework target into their app.

苹果在安装iOS框架时开箱即用的体验令人非常渴望。 最初的集成商需要获取您的代码的副本。 然后,他们需要将其添加到其工作区中。 最后,他们需要将框架目标嵌入到他们的应用程序中。

This isn’t a great experience. Once you have a new version of your framework the integrator will also have to update their copy of the source code.

这不是一个很好的经验。 一旦有了新版本的框架,集成商还必须更新其源代码副本。

So is there a better way to integrate with iOS frameworks? Yes there is! Carthage is an open source tool that help iOS app developers manage their dependencies.

那么,有没有更好的方法与iOS框架集成? 就在这里! Carthage是一个开源工具,可帮助iOS应用程序开发人员管理其依赖项。

In this post we’ll learn:

在这篇文章中,我们将学习:

  • how Carthage works

    迦太基如何运作
  • how to distribute an iOS framework for Carthage

    如何为Carthage发行iOS框架
  • how to consume a framework distributed through Carthage

    如何使用通过迦太基分发的框架

Each section is independently consumable. Feel free to skip any section as necessary.

每个部分都是独立消耗的。 请根据需要随时跳过任何部分。

I assume the reader is familiar with Git version control and has knowledge on iOS frameworks. I also assume the reader has basic knowledge of Swift, iOS development and iOS frameworks.

我假设读者熟悉Git版本控制并且对iOS框架有所了解。 我还假定读者具有Swift,iOS开发和iOS框架的基本知识。

I have used Swift 5.2 and Xcode 11.4.1 for this post.

我已经为这篇文章使用了Swift 5.2和Xcode 11.4.1。

迦太基如何运作? (How does Carthage work?)

Carthage makes use of git version control and, Xcode project files and schemes to discover which version of the code to use and how to build it.

迦太基利用git版本控制以及Xcode项目文件和方案来发现要使用的代码版本以及如何构建它。

For publishing frameworks that works with Carthag the tool is not required. However to make it work the Xcode project and git repository must be used correctly for Carthage to discover the framework and build it.

对于与Carthag兼容的发布框​​架,不需要该工具。 但是,要使其正常工作,必须正确使用Xcode项目和git存储库,以使Carthage发现并构建该框架。

Below you can find a basic diagram of how Carthage works:

您可以在下面找到有关迦太基工作原理的基本示意图:

Image for post
How Carthage works
迦太基如何运作

如何使用Carthage分发Swift框架 (How to distribute Swift frameworks using Carthage)

In this section we’ll distribute an iOS framework through Carthage. The framework will be provided as creating a framework is out of scope for this tutorial.

在本节中,我们将通过迦太基分发一个iOS框架。 将提供该框架,因为创建框架超出了本教程的范围。

The steps we’ll take:

我们将采取的步骤:

  1. Retrieve the starter project

    检索入门项目
  2. Create local git repository

    创建本地git存储库
  3. Publish Swift framework version

    发布Swift框架版本

1.检索启动项目 (1. Retrieve the starter project)

Let’s retrieve the starter project. Open terminal and run the following commands:

让我们检索入门项目。 打开终端并运行以下命令:

cd $HOME
curl https://github.com/anuragajwani/distributing_ios_frameworks_carthage/archive/starter.zip -o ios_framework.zip -L -s
unzip -q ios_framework.zip
cd distributing_ios_frameworks_carthage-starter/MyFramework

The starter project contains a single iOS framework target with a single build scheme which is shared.

入门项目包含一个iOS框架目标和一个共享的构建方案。

Note: the scheme must be shared for Carthage to be able to discover the framework.

注意:必须共享该方案,Carthage才能发现该框架。

Image for post

The framework code contains a login screen which we’ll display to the user from within the integrator’s app.

框架代码包含一个登录屏幕,我们将从集成商的应用程序中向用户显示该屏幕。

2.创建本地git存储库 (2. Create local git repository)

Next we’ll publish our framework so that Carthage is able to consume it. Carthage leverages Git version control to distribute and import frameworks.

接下来,我们将发布我们的框架,以便迦太基能够使用它。 迦太基利用Git版本控制来分发和导入框架。

Let’s create a git repository for our framework. Execute the following commands:

让我们为我们的框架创建一个git存储库。 执行以下命令:

git init
git add --all
git commit --message "Added my framework project"

In this post we have created a local repository. We’ll be referring to this local repository as the one that will be reached by the integrator. However most likely you’ll need to use a git repository hosting service on the internet (i.e. GitHub) so your integrators can reach your code.

在这篇文章中,我们创建了一个本地存储库。 我们将把这个本地存储库称为集成商将要访问的存储库。 但是,最有可能您需要在Internet(即GitHub )上使用git存储库托管服务,以便您的集成商可以访问您的代码。

3.发布Swift框架版本 (3. Publish Swift framework version)

Carthage uses Git tags to determine which version of your code use. The tags must follow Semantic Versioning specification; this is how you decide whether to tag your version as 1.0.1 or 1.1.0. We won’t be covering Semantic Versioning specification in this post. We’ll only publish a single version of our iOS framework; 1.0.0. Execute the following command:

迦太基使用Git标签来确定使用哪个版本的代码。 标签必须遵循语义版本规范; 这是决定如何将版本标记为1.0.11.1.0 。 在这篇文章中,我们将不讨论语义版本控制规范。 我们只会发布iOS框架的单个版本; 1.0.0 。 执行以下命令:

git tag -a 1.0.0 -m "Version 1.0.0"

And that’s it! You are now able to import the framework using Carthage. Let’s see how to integrate with it in the next section.

就是这样! 现在,您可以使用Carthage导入框架。 在下一部分中,让我们看看如何与它集成。

如何使用Carthage消费iOS框架 (How to consume iOS frameworks using Carthage)

In this section we’ll be consuming an iOS framework framework from the previous section. We’ll create an app from scratch.

在本节中,我们将使用上一节中的iOS框架框架。 我们将从头开始创建一个应用。

The steps we’ll take:

我们将采取的步骤:

  1. Create a new iOS app

    创建一个新的iOS应用
  2. Install Carthage

    安装迦太基
  3. Create a Cartfile

    创建一个Cartfile
  4. Fetch dependencies

    获取依赖
  5. Link dependencies

    链接依赖
  6. Consuming the framework

    消耗框架

1.创建一个新的iOS应用 (1. Create a new iOS app)

Let’s create an app that will consume our Swift package. Open Xcode and from menu select File > New > Project…

让我们创建一个使用Swift包的应用程序。 打开Xcode,然后从菜单中选择文件 > 新建 > 项目…

Image for post

When prompted “Choose a template for new project:” select Single View App and then click Next.

当出现提示“ 选择新项目的模板: ”时,选择“ Single View App” ,然后单击“ 下一步”

Image for post

On new project options name the product CarthageDemo. Select the language as Swift. Select Storyboard as the “User Interface” option. Keep all checkboxes unchecked. Click Next.

在新的项目选项上,将产品命名为CarthageDemo 。 选择语言为Swift 。 选择Storyboard作为“ 用户界面 ”选项。 取消选中所有复选框。 单击下一步

Image for post

When prompted where to save it use the keys ⌘ + ⇧ + G (Command + Shift + G). When prompted “Go to the folder” enter ~/distributing_ios_frameworks_carthage-starter/ then click Go.

当提示保存位置时,使用use +⇧+ G键(Command + Shift + G)。 当提示“ 转到文件夹 ”时,输入~/distributing_ios_frameworks_carthage-starter/然后单击“ 转到”

Image for post

Finally click Create.

最后单击创建

2.安装迦太基 (2. Install Carthage)

Before we can install our iOS framework using Carthage we need to first install Carthage itself. Open terminal and execute the following commands:

在使用Carthage安装iOS框架之前,我们需要先安装Carthage本身。 打开终端并执行以下命令:

cd $HOME
curl -L https://github.com/Carthage/Carthage/releases/download/0.34.0/Carthage.pkg -o Carthage.pkg
sudo installer -pkg Carthage.pkg -target /usr/local

You’ll need to enter your macOS user password to install Carthage in /usr/local.

您需要输入macOS用户密码才能在/usr/local安装Carthage。

3.创建一个Cartfile (3. Create a Cartfile)

Next we’ll specify Carthage what dependencies our framework requires. The way to this is by creating a file named Cartfile which will contain a list of dependencies for our project. Execute the following commands:

接下来,我们将指定Carthage我们的框架需要哪些依赖项。 解决方法是创建一个名为Cartfile的文件,该文件将包含我们项目的依赖项列表。 执行以下命令:

cd ~/distributing_ios_frameworks_carthage-starter/CarthageDemo
touch Cartfile

Next declare the MyFramework dependency. Execute the following command:

接下来声明MyFramework依赖项。 执行以下命令:

cat > Cartfile <<-EOF
git "file://$HOME/distributing_ios_frameworks_carthage-starter/MyFramework" "1.0.0"
EOF

In case of using a remote git repository just change the url between double quotes with the desired git url.

如果使用远程git仓库,只需将双引号之间的url更改为所需的git url。

4.提取依赖项 (4. Fetch dependencies)

Next let’s tell Carthage to fetch our dependencies. Execute the following command:

接下来,让我们告诉迦太基获取我们的依赖。 执行以下命令:

carthage update
Image for post
terminal output for “carthage update”
终端输出“迦太基更新”

Notice that Carthage fetches and builds the dependencies. The build is a universal framework that works with devices and simulators. Xcode doesn’t offer this out of the box. However Carthage doesn’t link the built framework with our project. We’ll do that in the next step.

注意,迦太基获取并构建了依赖关系。 该版本是与设备和模拟器一起使用的通用框架。 Xcode不提供此功能。 但是,迦太基并未将构建的框架与我们的项目链接。 我们将在下一步中进行操作。

5.链接依赖 (5. Link dependencies)

Next we’ll need to link MyFramework to CarthageDemo. We have to do that in the CarthageDemo project settings.

接下来,我们需要将MyFramework链接到CarthageDemo 。 我们必须在CarthageDemo项目设置中执行此操作。

First let’s open project settings. Open project navigator (from menu select View > Navigators > Show Project Navigator).

首先,我们打开项目设置。 打开项目导航器(从菜单中选择“ 视图” >“ 导航器” >“ 显示项目导航器” )。

Image for post

Next select CarthageDemo project file (at the top level with the blue icon).

接下来,选择CarthageDemo项目文件(在最顶层带有蓝色图标)。

Image for post

Then select the CarthageDemo app target.

然后选择CarthageDemo应用程序目标。

Image for post

Next select the General tab and within the main pane scroll down to “Frameworks, Libraries and Embedded Content”.

接下来,选择“ General选项卡,然后在主窗格中向下滚动到“ Frameworks,Libraries和Embedded Content ”。

Image for post

Here we’ll drag and drop the framework. We need to locate the framework and drag and drop it in “Frameworks, Libraries and Embedded Content”.

在这里,我们将拖放框架。 我们需要找到框架并将其拖放到“ Frameworks,Libraries和Embedded Content ”中。

To open the location of the framework execute the following command in terminal:

要打开框架的位置,请在终端中执行以下命令:

open -a Finder ~/distributing_ios_frameworks_carthage-starter/CarthageDemo/Carthage/Build/iOS/

Next drag and drop MyFramework.framework into “Frameworks, Libraries, and Embedded Content

接下来,将MyFramework.framework拖放到“ Frameworks,Libraries和Embedded Content

Image for post

And that’s it the framework is now linked to the app.

就是这样,框架现已链接到应用程序。

6.消耗框架 (6. Consuming the framework)

In the final step we’ll consume the frameworks functionality. We’ll be presenting the login screen within the framework when the user taps on the login button.

在最后一步,我们将使用框架功能。 当用户点击登录按钮时,我们将在框架内显示登录屏幕。

First let’s import the framework in our ViewController. Open ViewController.swift and add the following line under import UIKit:

首先,让我们在ViewController导入框架。 打开ViewController.swift并在import UIKit下添加以下行:

import MyFramework

Next let’s add functions to create the login button and when tapped present the login screen:

接下来,让我们添加创建登录按钮的功能,并在点击时显示登录屏幕:

private func addButton() {let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = falsebutton.setTitle("Login", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(self.login), for: .touchUpInside)self.view.addSubview(button)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])
}@objc private func login() {let loginVC = LoginViewController()self.present(loginVC, animated: true, completion: nil)
}

Finally let’s add the button by calling the addButton function. Add the following line at the end of viewDidLoad:

最后,让我们通过调用addButton函数来添加按钮。 在viewDidLoad的末尾添加以下行:

self.addButton()

That’s it! Run the app and see it in action 🎉.

而已! 运行该应用程序,然后在操作see中查看它。

Image for post

摘要 (Summary)

In this post we learnt’t:

在这篇文章中,我们不了解:

  • how Carthage works

    迦太基如何运作
  • how to publish a framework for Carthage

    如何发布迦太基框架
  • how to consume a framework using Carthage

    如何使用迦太基使用框架

最后的笔记 (Final notes)

You can find the full source code below:

您可以在下面找到完整的源代码:

Publishing and consuming frameworks using Carthage is very simple. However Carthage is not the only dependency manager for iOS. The most popular dependency management tool for iOS is Cocoapods. Additionally Swift Package Manager is the latest dependency management tool to join the group. Swift Package Manager is bundled with Swift. However Swift Package Manager does not support framework distribution only libraries for now.

使用Carthage发布和使用框架非常简单。 但是,迦太基不是iOS的唯一依赖项管理器。 iOS上最流行的依赖项管理工具是Cocoapods 。 另外, Swift Package Manager是加入该小组的最新依赖管理工具。 Swift软件包管理器与Swift捆绑在一起。 但是,Swift Package Manager目前不支持仅框架分发库。

A full comparison of the advantages and disadvantages of each tool is not in scope for this post. Later I’ll publish a post comparing the most popular tools.

每种工具的优缺点的全面比较不在本文的讨论范围之内。 稍后,我将发表一篇文章,比较最受欢迎的工具。

Stay tuned for more on iOS development! Follow me on Twitter or Medium.

请继续关注有关iOS开发的更多信息! 在TwitterMedium上关注我。

翻译自: https://medium.com/@anuragajwani/how-to-distribute-ios-frameworks-using-carthage-e47ef77e2fc4

迦太基 oc

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值