使用swift软件包管理器分发ios swift库

There are a few ways of distributing Swift libraries to integrators, CocoaPods being one of the most popular ones. Until recently, the tools used are ones created and maintained by the open-source community. With Swift 3 Apple, the Swift community was introduced to the Swift Package Manager.

有几种将Swift库分发给集成商的方法, CocoaPods是最受欢迎的方法之一。 直到最近,使用的工具还是由开源社区创建和维护的工具。 在Swift 3 Apple中,Swift社区被引入了Swift Package Manager。

In this article, we’ll cover why you should support Swift Package Manager. Then we’ll go through the steps on how to distribute and consume static libraries using the Swift Package Manager on an iOS app.

在本文中,我们将介绍为什么您应该支持Swift Package Manager。 然后,我们将介绍如何使用iOS应用程序上的Swift Package Manager分发和使用静态库的步骤。

I assume the reader is already familiar with Swift, Swift static libraries, and Xcode. I also assume the reader is familiar with the Git version control system and GitHub.

我假设读者已经熟悉Swift,Swift静态库和Xcode。 我还假定读者熟悉Git版本控制系统和GitHub 。

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

我已经在本文中使用了Swift 5.2和Xcode 11.4.1。

为什么要以Swift软件包的形式发布库 (Why Should You Publish Your Library as a Swift Package)

There are dependency/package managers already supported by the open-source community, so why do we need another one?

开源社区已经支持了依赖性/程序包管理器,那么为什么我们需要另一个呢?

First, existing tools have to be downloaded, maintained, and installed separately. Swift Package Manager comes included with Swift and Xcode. That means no additional installation required.

首先,必须分别下载,维护和安装现有工具。 Swift套件管理器随附于Swift和Xcode中。 这意味着不需要其他安装。

Second, like popular third-party tools, Swift Package Manager is open-source. However, most of the third-party tools make use of languages that are not Swift. Swift Package Manager is written in Swift. Being written in Swift makes it easier for the Swift developer to read, understand, and maybe even contribute to the project.

其次,像流行的第三方工具一样,Swift Package Manager是开源的 。 但是,大多数第三方工具都使用非Swift语言。 Swift Package Manager是用Swift编写的。 用Swift编写使Swift开发人员更容易阅读,理解甚至为该项目做贡献。

Third, Swift Package Manager is supported by the Xcode user interface. This means we can do the most operations without ever leaving Xcode. Other tools do not have native Xcode UI integration.

第三,Xcode用户界面支持Swift Package Manager。 这意味着我们无需离开Xcode就可以进行最多的操作。 其他工具没有本机Xcode UI集成。

Finally, Swift Package Manager is supported by Apple. In the past, new versions of Xcode have occasionally broken third-party tools integration. This meant that either you were not able to upgrade to the latest Xcode or you had to get rid of third-party tools that were no longer compatible. With Swift Package Manager being maintained by Apple, the tool runs less risk of breaking with newer versions of Xcode.

最后,Apple支持Swift Package Manager。 过去,新版本的Xcode偶尔会破坏第三方工具的集成。 这意味着您要么无法升级到最新的Xcode,要么必须摆脱不再兼容的第三方工具。 由于Apple维护了Swift Package Manager,因此该工具在使用较新版本的Xcode时被破坏的风险较小。

Thus Swift Package Manager:

因此,Swift软件包管理器:

  • Requires no additional download, installation, and maintenance of a third-party tool

    无需额外下载,安装和维护第三方工具
  • Makes it easy to get involved with the project

    使参与项目变得容易
  • Allows you to run the tool without leaving Xcode

    使您无需离开Xcode即可运行该工具
  • Is maintained by Apple

    由苹果公司维护

如何将静态库发布到Swift Package Manager (How to Publish a Static Library to Swift Package Manager)

In this section, we will publish an existing iOS static library as a Swift package. A GitHub account is required for publishing packages for this section.

在本节中,我们将现有的iOS静态库发布为Swift包。 发布此部分的软件包需要GitHub帐户。

In this section, we will:

在本节中,我们将:

  1. Retrieve starter project

    检索启动项目
  2. Create package declaration

    创建包声明
  3. Create a local Git repository

    创建本地Git存储库
  4. Set up a GitHub account

    设置GitHub帐户
  5. Publish to GitHub

    发布到GitHub

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

First, let’s download the starter project. Open terminal and execute the following commands:

首先,让我们下载入门项目。 打开终端并执行以下命令:

cd $HOME
curl https://github.com/anuragajwani/distributing_swift_lib_spm/archive/starter.zip -o swift_lib.zip -L -s
unzip -q swift_lib.zip
cd distributing_swift_lib_spm-starter/MyStaticLib

The starter project contains a Swift static library with a single file and function. The functionality of the library is not important for this article.

入门项目包含一个带有单个文件和功能的Swift静态库。 该库的功能对于本文而言并不重要。

2.创建包声明 (2. Create package declaration)

Next, let’s make our library a Swift package. Xcode has a convenient option to create a new package easily. However, Xcode does not have a convenient option to make an existing library a package. We’ll be creating our package declaration from scratch.

接下来,让我们的库成为Swift包。 Xcode有一个方便的选项,可以轻松地创建新程序包。 但是,Xcode没有方便的选项将现有库设置为包。 我们将从头开始创建包声明。

First, we’ll need to create a file named Package.swift. Go back to the terminal and execute the following command:

首先,我们需要创建一个名为Package.swift的文件。 返回终端并执行以下命令:

touch Package.swift

Next, we’ll create the package specification. Execute the following command:

接下来,我们将创建包装规范。 执行以下命令:

cat > Package.swift <<-EOF
// swift-tools-version:5.2
import PackageDescription
let package = Package(
    name: "MyStaticLib",
    products: [
        .library(name: "MyStaticLib", targets: ["MyStaticLib"])
    ],
    targets: [
        .target(name: "MyStaticLib", path: "MyStaticLib")
    ]
)
EOF

The first line of the Package.swift file (content betweenEOF) declares the minimum version of the Swift toolchain required to use this package. At the time of writing, I am using Xcode 11.4.1, which includes Swift toolchain version 5.2.2. You can find the toolchain version by running the following command:

Package.swift文件的第一行( EOF之间的内容)声明使用此软件包所需的Swift工具链的最低版本。 在撰写本文时,我正在使用Xcode 11.4.1,其中包括Swift工具链版本5.2.2 。 您可以通过运行以下命令找到工具链版本:

swift --version
getting the swift version 快速版本

The second line, import PackageDescription, declares the use of the Package framework. This is bundled already with Xcode and Swift.

第二行import PackageDescription声明了Package 框架的使用。 这已经与Xcode和Swift捆绑在一起。

Next is the package declaration itself.

接下来是包声明本身。

First is the name of the package itself. Next, let’s cover targets. A target covers a set of source files to be built. We can specify exactly which sources to include or exclude, or if not specified, then all eligible files under a path. In this case, we’ll compile all of the source files under the MyStaticLib directory.

首先是包本身的名称。 接下来,让我们介绍一下目标。 目标涵盖要构建的一组源文件。 我们可以精确指定要包含或排除的源,如果未指定,则可以指定路径下的所有合格文件。 在这种情况下,我们将编译MyStaticLib目录下的所有源文件。

Finally, let’s talk about products. We declare the products that the package will build. Currently, a product can be either a library or an executable. For this article, we’ll focus only on a library. Notice we have specified that the MyStaticLib product will build the MyStaticLib target. You can build a product comprised of multiple targets, but in this case, we only have a single target.

最后,让我们谈谈产品。 我们声明将要包装的产品 。 当前,产品可以是库或可执行文件。 对于本文,我们将仅关注库。 注意,我们已经指定MyStaticLib产品将构建MyStaticLib目标。 您可以构建一个包含多个目标的产品,但是在这种情况下,我们只有一个目标。

A quick note on executables: Use this if you’d like to build a command-line tool.

关于可执行文件的简要说明:如果要构建命令行工具,请使用此文件。

And that’s all that's needed to declare the package.

这就是声明包所需要的。

3.创建本地Git存储库 (3. Create a local Git repository)

Next, we’ll create a local Git repository of MyStaticLib. We need this as a step toward publishing our package in a remote repository that will be hosted in GitHub.

接下来,我们将创建MyStaticLib的本地Git存储库。 我们需要此步骤,以将我们的软件包发布到将托管在GitHub上的远程存储库中。

For this step, we’ll create the local Git repository though terminal. Open terminal and execute the following commands:

对于此步骤,我们将通过终端创建本地Git存储库。 打开终端并执行以下命令:

cd $HOME/distributing_swift_lib_spm-starter/MyStaticLib
git init
git add --all
git commit --message "Initial commit"

4.设置GitHub帐户 (4. Set up GitHub account)

Before continuing, make sure you have a GitHub account. Also, we’ll need to add the GitHub account to Xcode accounts. To link a GitHub account to Xcode, open Xcode and from the menu select Xcode > Preferences.

在继续之前,请确保您具有GitHub帐户。 另外,我们需要将GitHub帐户添加到Xcode帐户。 要将GitHub帐户链接到Xcode,请打开Xcode,然后从菜单中选择Xcode>首选项。

Then click on the Accounts tab. Next, click on the plus icon, “+”.

然后单击“帐户”选项卡。 接下来,单击加号图标“ + ”。

When prompted “Select the type of account you would like to add,” select GitHub and click Continue.

当出现提示“选择您要添加的帐户类型”时,选择GitHub并单击继续。

Next, you’ll need a personal access token to your GitHub account. To create one, follow these steps. When creating a personal access token, make sure you check the “repo” scope.

接下来,您需要一个到GitHub帐户的个人访问令牌。 要创建一个,请按照以下步骤操作 。 创建个人访问令牌时,请确保检查“回购”范围。

New personal access token creation 新的个人访问令牌创建

Then enter your account name and personal token access in the “Sign in to your GitHub account” prompt. Next click on Sign In.

然后在“登录到GitHub帐户”提示中输入您的帐户名和个人令牌访问权限。 接下来单击登录。

You can close the Preferences window now.

您现在可以关闭“首选项”窗口。

5.发布到GitHub (5. Publish to GitHub)

Next, let’s publish MyStaticLib to GitHub. We’ll do this through Xcode.

接下来,让我们将MyStaticLib发布到GitHub。 我们将通过Xcode做到这一点。

First, let’s open our Swift package in Xcode. Open terminal and execute the following command:

首先,让我们在Xcode中打开Swift包。 打开终端并执行以下命令:

open ~/distributing_swift_lib_spm-starter/MyStaticLib/Package.swift

Next, let’s create the Git repository in GitHub. From the menu, select View > Navigators > Show Source Control Navigator.

接下来,让我们在GitHub中创建Git存储库。 从菜单中,选择视图>导航器>显示源代码控制导航器。

In the navigator area, right-click “MyStaticLib master” and then click Create “MyStaticLib” Remote.

在导航器区域中,右键单击“ MyStaticLib master”,然后单击“创建“ MyStaticLib” Remote”。

When prompted “Create ‘MyStaticLib’ remote,” you can choose whether to keep this repository private or public. I am choosing Private for this article, as we’re only showcasing how to publish and consume Swift packages. Then click Create.

当提示“远程创建'MyStaticLib'时”,您可以选择将此存储库设置为私有还是公共。 我在本文中选择“私有”,因为我们仅展示了如何发布和使用Swift软件包。 然后单击创建。

Swift packages make use of Git tags to pull the desired version of the Swift package. Swift package versioning follows semantic versioning standards. Knowledge of semantic versioning is not required at this point, as we’ll only publish a single version.

Swift软件包利用Git标签提取所需版本的Swift软件包。 Swift软件包版本控制遵循语义版本控制标准。 目前不需要语义版本控制知识,因为我们将只发布一个版本。

Let’s create a Git tag for our first version. Right-click again “MyStaticLib master” in the navigator area and then click “Tag ‘master’.”

让我们为第一个版本创建一个Git标签。 再次在导航器区域中右键单击“ MyStaticLib master”,然后单击“ Tag'master'”。

When prompted “Create a new tag from revision,” input 1.0.0 into the Tag field. Next, click Create.

当提示“从修订版创建新标签”时,在“标签”字段中输入1.0.0 。 接下来,单击创建。

The tag is now created locally. Let’s push this tag to the remote Git repository in GitHub. From the menu select Source Control > Push.

标签现在在本地创建。 让我们将此标签推送到GitHub中的远程Git存储库。 从菜单中选择“源代码控制”>“推入”。

When prompted “Push local changes to,” check the “Include tags” checkbox. Then click Push.

当系统提示“将本地更改推送到”时,选中“包含标签”复选框。 然后单击“推送”。

Now the tag is available in our GitHub repository. MyStaticLib Swift package is now available!

现在,该标记在我们的GitHub存储库中可用。 MyStaticLib Swift软件包现已可用!

如何使用已发布的静态库 (How to Consume the Published Static Library)

In this section, we will learn how to consume the Swift package we created in the previous section.

在本节中,我们将学习如何使用上一节中创建的Swift包。

In this section we will:

在本节中,我们将:

  1. Create an iOS app to consume MyStaticLib

    创建一个iOS应用以使用MyStaticLib

  2. Add MyStaticLib Swift package to the app

    MyStaticLib Swift软件包添加到应用程序

  3. Consume MyStaticLib

    消耗MyStaticLib

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

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

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

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

When prompted “Choose options for your new project,” name your product SwiftPackageDemo in the Product Name field. All other options can be left as is. Click Next.

当提示“选择新项目的选项”时,在“产品名称”字段中将产品命名为SwiftPackageDemo 。 其他所有选项都可以保留。 点击下一步。

Save it wherever you think appropriate. Click Create.

保存在您认为合适的任何地方。 单击创建。

Next from menu, select File > Swift Packages > Add Package Dependency.

接下来,从菜单中选择文件> Swift软件包>添加软件包依赖项。

When prompted “Choose Package Repository,” search and select MyStaticLib, which you are the owner of. Click Next.

当出现提示“选择软件包存储库”时,搜索并选择MyStaticLib ,它是您的所有者。 点击下一步。

When prompted “Choose Package Optionsk” Xcode will have already selected version 1.0.0 for you. Click Next.

当提示“选择软件包选项k”时,Xcode将为您选择版本1.0.0 。 点击下一步。

When prompted “Add Package to SwiftPackageDemo,” make sure MyStaticLib is checked for the SwiftPackageDemo app. Click Finish.

当系统提示“添加包到SwiftPackageDemo,”确保MyStaticLib被选中为SwiftPackageDemo应用。 单击完成。

MyStaticLib has now been added as a dependency of the SwiftPackageDemo app.

MyStaticLib现在已添加为SwiftPackageDemo应用程序的依赖项。

Next, you’ll use MyStaticLib code in our app. Open AppDelegate.swift and after import UIKit, add the following line:

接下来,您将在我们的应用程序中使用MyStaticLib代码。 打开AppDelegate.swift ,然后import UIKit ,添加以下行:

import MyStaticLib

Next, add the following line to the function application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool, before return true:

接下来, application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool添加到函数application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool ,然后return true

functionA()

Finally, run the app in a simulator.

最后,在模拟器中运行该应用程序。

You should now see the function from the static library printing a message to console. And that's all!

现在,您应该看到静态库中的函数将消息打印到控制台。 就这样!

摘要 (Summary)

In this article, you have learned:

在本文中,您了解了:

  • Why you should publish your library as a Swift package

    为什么要以Swift软件包的形式发布库
  • How to publish Swift packages

    如何发布Swift软件包
  • How to consume Swift packages

    如何使用Swift软件包

最后的笔记 (Final notes)

You can find the full source code for this article on GitHub.

您可以在GitHub上找到本文的完整源代码 。

In this article, we have talked about publishing a static library as a Swift package. However, what about Swift dynamic frameworks? We are able to publish Swift packages as dynamic libraries by looking at the documentation. We can do this by changing the library type. However, I haven’t tried this myself.

在本文中,我们讨论了将静态库发布为Swift程序包。 但是,Swift动态框架又如何呢? 通过查看文档,我们可以将Swift软件包发布为动态库。 我们可以通过更改库类型来实现 。 但是,我自己还没有尝试过。

Another noteworthy limitation to mention about Swift Package Manager is that we aren’t able to publish dynamic frameworks. What are the implications? In short, we can’t bundle resources such as images, XIBs, or storyboard files.

关于Swift Package Manager的另一个值得注意的限制是我们无法发布动态框架。 这意味着什么? 简而言之,我们不能捆绑资源,例如图像,XIB或情节提要文件。

What about distributing compiled static libraries or compiled dynamic frameworks with Swift Package Manager? We can’t do that either as of yet. However, the feature is being worked on.

如何使用Swift Package Manager分发已编译的静态库或已编译的动态框架? 到目前为止,我们也无法做到这一点。 但是, 该功能正在开发中 。

Thus Swift Package Manager can’t:

因此,Swift软件包管理器不能:

  • distribute resources

    分配资源
  • build dynamic frameworks

    建立动态框架
  • distribute compiled libraries

    分发编译的库

翻译自: https://medium.com/better-programming/distributing-ios-swift-libraries-with-swift-package-manager-3c9630149bb3

你可能感兴趣的:(使用swift软件包管理器分发ios swift库)