迦太基 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框架时,安装过程可能会有所不同,具体取决于框架的交付形式。 交付框架的两种形式是:
- source code 源代码
- 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](https://i-blog.csdnimg.cn/blog_migrate/6acfdea01ea17c5b88ea4be2526a3274.png)
如何使用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:
我们将采取的步骤:
- Retrieve the starter project 检索入门项目
- Create local git repository 创建本地git存储库
- 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](https://i-blog.csdnimg.cn/blog_migrate/afb6876a84605af2aa2d8b85112d48e1.png)
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.1
或1.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:
我们将采取的步骤:
- Create a new iOS app 创建一个新的iOS应用
- Install Carthage 安装迦太基
- Create a Cartfile 创建一个Cartfile
- Fetch dependencies 获取依赖
- Link dependencies 链接依赖
- 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](https://i-blog.csdnimg.cn/blog_migrate/8169cfb570ed6838a8a6964d9188d7a9.png)
When prompted “Choose a template for new project:” select Single View App and then click Next.
当出现提示“ 选择新项目的模板: ”时,选择“ Single View App” ,然后单击“ 下一步” 。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/b3f6b4c1b9abbbc5a3ea4e90f54e209d.png)
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](https://i-blog.csdnimg.cn/blog_migrate/b8d6e8262deec03708f0abb5f84592b1.png)
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](https://miro.medium.com/max/9999/1*syLNLdz2AAQwlB6fBtPx8g.png)
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](https://i-blog.csdnimg.cn/blog_migrate/6c1bd9512d80f2554c8ac3e790ab4682.png)
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](https://i-blog.csdnimg.cn/blog_migrate/2d4587af0b48063a759d601cb52aebd0.png)
Next select CarthageDemo
project file (at the top level with the blue icon).
接下来,选择CarthageDemo
项目文件(在最顶层带有蓝色图标)。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/cb40adacfa2729800eba831df89b083a.png)
Then select the CarthageDemo
app target.
然后选择CarthageDemo
应用程序目标。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/cd39762e5bb00d5c5e2364035529d9e0.png)
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](https://i-blog.csdnimg.cn/blog_migrate/ef902c43a180aecd72e5db53a1700830.png)
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](https://i-blog.csdnimg.cn/blog_migrate/cfd418d7be2b81a8571fe90c4ccbf5e6.png)
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](https://i-blog.csdnimg.cn/blog_migrate/648c272d6c6b7ecbe47cba88a57dfdae.png)
摘要 (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.
翻译自: https://medium.com/@anuragajwani/how-to-distribute-ios-frameworks-using-carthage-e47ef77e2fc4
迦太基 oc