ios布局框架 swift_如何使用Swift Package Manager分发已编译的iOS框架

ios布局框架 swift

模块化iOS (Modular iOS)

Do you want to distribute your iOS framework to other developers compiled? You may want to hide you source code. Maybe you want to save integrators from having to compile it.

您想将您的iOS框架分发给其他编译的开发人员吗? 您可能想要隐藏您的源代码。 也许您希望使集成商不必进行编译。

The release of Xcode 12 beta adds a new way for you to distribute your compiled iOS framework — that is using Swift Package Manager. However Swift Package Manager does not directly support distribution of frameworks themselves but rather frameworks wrapped inside of an XCFramework.

Xcode 12 beta发行版为您分发经编译的iOS框架提供了一种新方法,即使用Swift Package Manager。 但是,Swift软件包管理器并不直接支持框架本身的分发,而是直接包装在XCFramework内部的框架。

In this post I won’t go much in detail of what XCFrameworks are and how to build them. In this post I’ll cover how to distribute these and consume them using Swift Package Manager.

在这篇文章中,我不会详细介绍什么是XCFrameworks以及如何构建它们 。 在这篇文章中,我将介绍如何使用Swift Package Manager分发和使用它们。

I assume you are already familiar with iOS frameworks, how to build these and XCFrameworks. Furthermore you must be familiar with iOS development, git version control and Github. Another pre-requisite is to have your Github account connected to Xcode.

我假设您已经熟悉iOS框架 ,如何构建这些框架XCFrameworks 。 此外,您必须熟悉iOS开发, git版本控制Github 。 另一个前提条件是将您的Github帐户连接到Xcode

Note Xcode 12.0 beta is required for this post.

注意这篇文章需要Xcode 12.0 beta。

为Swift Package Manager发布已编译的iOS框架 (Publishing compiled iOS frameworks for Swift Package Manager)

In this section we’ll start by downloading an existing iOS framework project. Next we’ll build the framework and wrap it in an XCFramework. Then we’ll create a package specification. Finally we’ll publish the package specification and XCFramework to Github.

在本节中,我们将从下载现有的iOS框架项目开始。 接下来,我们将构建框架并将其包装在XCFramework中。 然后,我们将创建一个包装规范。 最后,我们将包规范和XCFramework发布到Github。

The steps we’ll take:

我们将采取的步骤:

  1. Retrieve the starter pack

    检索入门包
  2. Building the XCFramework

    建立XCFramework
  3. Create git repository to host the XCFramework

    创建git存储库以托管XCFramework
  4. Add the XCFramework to git repository

    将XCFramework添加到git存储库
  5. Create Package specification

    创建包装规格
  6. Publish package specification to the git repository

    将软件包规范发布到git存储库
  7. Push copy of the git repository to Github

    将git仓库的副本推送到Github

1.检索入门包 (1. Retrieve the starter pack)

Let’s start by downloading the starter Xcode project. Open the Termainl app and execute the following commands:

让我们从下载入门Xcode项目开始。 打开Termainl应用并执行以下命令:

cd $HOME
curl https://github.com/anuragajwani/swiftpm-xcframework-distribution/archive/starter.zip -o swiftpm-xcframework-tut-starter.zip -L -s
unzip -q swiftpm-xcframework-tut-starter.zip
cd swiftpm-xcframework-distribution-starter/MyFramework

The starter pack contains a iOS framework project and an iOS app project. The framework contains a single Login screen. The app currently display a Login button that doesn’t do anything. In the last step we’ll present the Login screen from the framework when the user taps the login button.

该入门包包含一个iOS框架项目和一个iOS应用程序项目。 该框架包含一个“登录”屏幕。 该应用程序当前显示“登录”按钮,该按钮不执行任何操作。 在最后一步,我们将在用户点击登录按钮时从框架中显示“登录”屏幕。

2.构建XCFramework (2. Building the XCFramework)

The starter pack’s framework project also includes a script to build an XCFramework. Knowledge of how to build XCFrameworks from frameworks is not required for this post.

入门包的框架项目还包括用于构建XCFramework的脚本。 这篇文章不需要有关如何从框架构建XCFrameworks的知识。

Let’s build the XCFramework. Execute the following command:

让我们构建XCFramework。 执行以下命令:

sh build_xcframework.sh

The script above builds the framework for iOS devices and simulators and then wrap these into an XCFramework. Upon successful execution the script will output a file named MyFramework.xcframework inside the build directory where the script is located.

上面的脚本为iOS设备和模拟器构建了框架,然后将它们包装到XCFramework中。 成功执行后,脚本将在脚本所在的构建目录内输出名为MyFramework.xcframework的文件。

3.创建git仓库来托管XCFramework (3. Create git repository to host the XCFramework)

Next we’ll create a git repository to host the XCFramework. In this tutorial we’re using git to version and track our XCFramework releases. Execute the following commands:

接下来,我们将创建一个git存储库来托管XCFramework。 在本教程中,我们使用git来版本控制和跟踪我们的XCFramework版本。 执行以下命令:

cd ~
mkdir MyFrameworkDistribution.git
cd MyFrameworkDistribution.git
git init --bare
git clone ~/MyFrameworkDistribution.git ~/MyFrameworkDistribution
cd ~/MyFrameworkDistribution
touch README.md
git add README.md
git commit -m "Initial commit"
git push origin -u master

4.将XCFramework添加到git存储库 (4. Add the XCFramework to git repository)

Next we’ll add the XCFramework to the local git repository. Execute the following commands:

接下来,我们将XCFramework添加到本地git存储库中。 执行以下命令:

cp -r ~/swiftpm-xcframework-distribution-starter/MyFramework/MyFramework.xcframework ~/MyFrameworkDistribution/
cd ~/MyFrameworkDistribution
git add --all
git commit -m "Added XCFramework"

5.创建包装规格 (5. Create Package specification)

Next we’ll create the Package specification inside the git repository. The package specification is a file that lets Swift Package Manager know how to consume the package — in this case the XCFramework. Execute the following command:

接下来,我们将在git存储库中创建Package规范。 软件包规范是一个文件,可让Swift软件包管理器知道如何使用该软件包-在本例中为XCFramework。 执行以下命令:

cat > ~/MyFrameworkDistribution/Package.swift <<-EOF
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "MyFramework",
platforms: [
.iOS(.v11)
],
products: [
.library(
name: "MyFramework",
targets: ["MyFramework"])
],
targets: [
.
name: "MyFramework",
path: "MyFramework.xcframework")
])
EOF

The most important bit to note for this post is:

这篇文章要注意的最重要的一点是:

.
name: "MyFramework",
path: "MyFramework.xcframework")

This was specifically added in Swift 5.3 and is the one that allows publishing and consuming compiled source using Swift Package Manager. Here we are saying that the target is compiled, the name of the target and where it lives within our repository.

它是在Swift 5.3中专门添加的,并且允许使用Swift Package Manager发布和使用已编译的源代码。 在这里,我们说的是目标已编译,目标的名称及其在我们存储库中的位置。

If you’d like to learn more about rest of Package.swift you can find more about it in my previous post or in Apple’s official documentation site.

如果您想进一步了解Package.swift其余部分,可以在我以前的文章Apple的官方文档站点中找到有关它的更多信息。

6.将软件包规范发布到git存储库 (6. Publish package specification to the git repository)

Next we need to publish the package specification to the local git repository. Execute the following commands:

接下来,我们需要将软件包规范发布到本地git存储库。 执行以下命令:

cd ~/MyFrameworkDistribution
git add Package.swift
git commit -m "Added package specification"

Finally we need to tag this release of our XCFramework. The tag is how Swift Package Manager on the integrators app will find the version the integrator desires to install. Let’s say the integrator wishes to install version 1.1.0 of you XCFramework. Swift Package Manager will look through the tags and use version that matches the tag 1.1.0.

最后,我们需要标记此版本的XCFramework。 标记是集成商应用程序上的Swift Package Manager如何找到集成商希望安装的版本的方式。 假设集成商希望安装XCFramework的1.1.0版本。 Swift Package Manager将浏览标签并使用与标签1.1.0匹配的版本。

For this first release of our XCFramework we’ll tag it 1.0.0. Execute the following command:

对于XCFramework的第一个版本,我们将其标记为1.0.0 。 执行以下命令:

git tag -a 1.0.0 -m "Version 1.0.0"

7.将git仓库的副本推送到Github (7. Push copy of the git repository to Github)

Next we’ll push a copy of our git repository to Github. For such we’ll need to create a new repository on Github. Click on the link below:

接下来,我们将git存储库的副本推送到Github。 为此,我们需要在Github上创建一个新的存储库。 点击下面的链接:

Next name the repository as MyFrameworkDistribution. You can make your repo private if you like. Click on Create Repository.

接下来将存储库命名为MyFrameworkDistribution 。 您可以根据需要将自己的回购设置为私有。 单击创建存储库

Image for post

Next let’s push a copy of our local git repository to the Github repository just created. Back in Terminal execute the following commands (remember to replace <YOUR_GITHUB_USERNAME> with your Github username):

接下来,让我们将本地git存储库的副本推送到刚刚创建的Github存储库。 返回终端,执行以下命令(请记住用您的Github用户名替换<YOUR_GITHUB_USERNAME> ):

cd ~/MyFrameworkDistribution
git remote add github git@github.com:<YOUR_GITHUB_USERNAME>/MyFrameworkDistribution.git
git push github -u master
git push github --tags

And that is it. Our XCFramework is now published and ready to be consumed by Swift Package Manager!

就是这样。 我们的XCFramework现在已经发布,可以由Swift Package Manager使用!

使用Swift Package Manager消耗XCFramework (Consuming XCFramework using Swift Package Manager)

In this section we’ll consume the XCFramework we published in the previous section. We’ll be using an iOS app project already included within the starter pack we downloaded in the previous section.

在本节中,我们将使用上一节中发布的XCFramework。 我们将使用上一节中下载的入门包中已经包含的iOS应用程序项目。

The steps we’ll take:

我们将采取的步骤:

  1. Import XCFramework to the app

    将XCFramework导入到应用程序
  2. Consume the XCFramework code

    消耗XCFramework代码

1.将XCFramework导入到应用程序 (1. Import XCFramework to the app)

First let’s open the app included in the starter pack. Execute the following command:

首先,让我们打开入门包中包含的应用程序。 执行以下命令:

open -a Xcode-beta ~/swiftpm-xcframework-distribution-starter/SwiftPMXCFrameworkDemo/SwiftPMXCFrameworkDemo.xcodeproj

Note I have saved my copy of Xcode 12.0 beta in my Applications folder.

注意我已将Xcode 12.0 beta的副本保存在“应用程序”文件夹中。

Next let’s import the XCFramework. From menu select File > Swift Packages > Add Package Dependency…

接下来,我们导入XCFramework。 从菜单中选择文件 > Swift软件包 > 添加软件包依赖项…

Image for post

Next when prompted “Choose Package Repository” search and select MyFrameworkDistribution. Click Next.

接下来,当系统提示“ 选择软件包存储库 ”时,搜索并选择MyFrameworkDistribution 。 单击下一步

Image for post

When prompted to “Choose Package Options” leave the options default. XCode already fills out the version to 1.0.0 automatically. Click Next.

当提示“ 选择软件包选项 ”时,请保留默认选项。 XCode已经自动将版本填写为1.0.0 。 单击下一步

Image for post

Finally when prompted to “Add Package to SwiftPMXCFrameworkDemo:” make sure MyFramework Package is selected and the target destination (under “Add to Target” column) is SwiftPMXCFrameworkDemo. Click Finish.

最后,当系统提示您“ 将软件包添加到SwiftPMXCFrameworkDemo: ”时,请确保选择了MyFramework软件包,并且目标位置(在“添加到目标”列下)是SwiftPMXCFrameworkDemo 。 点击完成

Image for post

2.消费XCFramework代码 (2. Consume the XCFramework code)

For the last step of this tutorial we’ll consume the code contained within the XCFramework. Within Xcode open ViewController.swift and under import UIKit add the following line of code:

对于本教程的最后一步,我们将使用XCFramework中包含的代码。 在Xcode中,打开ViewController.swift并在import UIKit添加以下代码行:

import MyFramework

Next within the loginButtonTapped function add the following lines of code:

接下来,在loginButtonTapped函数中添加以下代码行:

let loginViewController = LoginViewController()self.present(loginViewController, animated: true, completion: nil)

And that is it. Run the code in any simulator or device and see it work! 🎉

就是这样。 在任何模拟器或设备中运行代码,然后查看其工作情况! 🎉

Image for post

摘要 (Summary)

In this post we learn’t:

在这篇文章中,我们不会:

  • how to distribute compiled frameworks (as XCFrameworks) through Swift Package Manager

    如何通过Swift Package Manager分发已编译的框架(作为XCFrameworks)
  • how to consume compiled frameworks (as XCFrameworks) through Swift Package Manager

    如何通过Swift Package Manager使用已编译的框架(作为XCFrameworks)

最后说明 (Final Notes)

For a long time your only option to distribute compiled iOS frameworks was by using an third-party dependency managers such as Cocoapods or Carthage. Furthermore building universal iOS frameworks — one which works with simulators and devices in one artefact — was not supported by Xcode out of the box.

长期以来,分发经编译的iOS框架的唯一选择是使用第三方依赖性管理器,例如Cocoapods或Carthage。 此外,开箱即用的Xcode不支持构建通用的iOS框架 (可以在一个工件中与模拟器和设备一起使用)。

Since Xcode 11.0 Apple gave us the ability to distribute universally compatible frameworks through XCFrameworks. With Xcode 12.0 the Swift community and Apple have brought us the capability of distributing XCFrameworks through SwiftPM.

从Xcode 11.0开始,Apple允许我们通过XCFrameworks分发通用兼容的框架。 借助Xcode 12.0,Swift社区和Apple为我们带来了通过SwiftPM分发XCFrameworks的功能。

I have been maintaining and distributing a compiled iOS framework for nearly 4 years now. Until now I have felt that many of the solutions I applied to distribute the compiled framework was hacky and brittle. Every WWDC I wondered what would break next. However its now possible that these will experiences of the past! WWDC 2020 is possibly my favourite WWDC ever ♥️

我一直在维护和分发已编译的iOS框架近4年。 到现在为止,我感觉到我用来分发已编译框架的许多解决方案都是很脆弱的。 我想知道每个WWDC接下来会发生什么。 但是,这些现在可能会成为过去! WWDC 2020可能是我最喜欢的WWDC♥️

翻译自: https://medium.com/@anuragajwani/how-to-distribute-compiled-universal-ios-xcframeworks-using-swift-package-manager-8eaf8395985f

ios布局框架 swift

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值