swift 封装sdk_在Swift中为iOS构建SDK

本文介绍了如何在Swift中封装SDK,为iOS应用开发提供便利。详细步骤包括设计SDK架构、实现关键功能以及最佳实践。
摘要由CSDN通过智能技术生成

swift 封装sdk

Recently, I was working on a project that would entail clients configuring and integrating our service with their existing mobile applications. At first, I thought it would be a simple enough process that they would be able to figure it out themselves with just some simple documentation. As we added more functionality, however, the work on their side became increasingly more complex to a point where we realized this was no longer a workable solution.

最近,我正在从事一个项目,该项目需要客户端配置我们的服务并将其与现有的移动应用程序集成。 起初,我认为这将是一个非常简单的过程,因此他们只需要一些简单的文档就可以自行解决。 但是,随着我们添加更多功能,他们身边的工作变得越来越复杂,以至于我们意识到这不再是可行的解决方案。

We realized that by continuing with our initial hands-off approach, we would be opening up the door to countless unforeseen issues down the road and potentially a sub-par end user experience if out parters incorrectly configured the application.

我们意识到,继续我们最初的放手方法,我们将为无数无法预料的问题打开大门,如果合作伙伴错误地配置了应用程序,则可能会降低最终用户的体验。

This led us to the eventual decision that the best approach would to build our own SDK that we would then be able to distribute to client — drastically reducing the time and effort spent by their development team, as well as reducing the scope of errors that might pop up during the initial setup process.

这导致我们最终决定,最好的方法是构建自己的SDK,然后将其分发给客户-大大减少了开发团队的时间和精力,并减少了可能导致错误的范围在初始设置过程中弹出。

This post will go through the process of setting up a simple iOS package, talk about some common pitfalls you might run into during the process, explore how to test locally, and finally publish to SPM (Swift Package Manager).

这篇文章将介绍设置一个简单的iOS程序包的过程,讨论在此过程中可能遇到的一些常见陷阱,探讨如何在本地进行测试,最后将其发布到SPM(快速程序包管理器)。

创建一个新包 (Creating a New Package)

Xcode (Xcode)

Image for post
Creating a new swift package with Xcode
使用Xcode创建新的Swift包

Creating a new package from inside Xcode takes about 5 seconds. Navigate to File > New > Swift Package, then save the package wherever you want.

从Xcode内部创建新程序包大约需要5秒钟。 导航到“ 文件”>“新建”>“ Swift软件包” ,然后将软件包保存在所需的位置。

命令行 (Command Line)

Alternatively, you can create a package from the command line by running:

另外,您可以通过运行以下命令从命令行创建程序包:

配置您的Swift软件包 (Configuring your Swift Package)

Swift packages don’t use .xcproject or .xcworkspace but rely on their folder structure and use the Package.swift file for additional configuration. The following code listing shows a simple package manifest

Swift软件包不使用.xcproject.xcworkspace而是依靠其文件夹结构并使用Package.swift文件进行其他配置。 以下代码清单显示了一个简单的包清单

代码 (The Code)

At this point you’ll have a file called MediumSDK.swift (or whatever your package is called) under the Sources directory. This is where we’ll store the code that needs to be included in our SDK.

此时,您将在Sources目录下有一个名为MediumSDK.swift的文件(或任何称为包的文件)。 在这里我们将存储需要包含在我们的SDK中的代码。

At this point, you would add whatever functionality you needed for your SDK here. For my project, I needed a custom preconfigured WKWebView that I could send to our partners. Here’s a stripped down version of something similar to what I ended up using.

此时,您将在此处添加SDK所需的任何功能。 对于我的项目,我需要一个可以发送给我们的合作伙伴的自定义预配置WKWebView。 这是我最终使用的类似内容的精简版。

You would then add this file under Sources/MediumSDK, or Sources/YourPackageNameHere. A couple things to note when adding a custom class like this:

然后,您可以在Sources / MediumSDKSources / YourPackageNameHere下添加此文件。 添加这样的自定义类时,需要注意以下几点:

  • Make sure your class (or protocol or function) is public or else your project that is importing it won’t be able to access it

    确保您的类(或协议或函数)是公共的 ,否则导入的项目将无法访问它

  • Inside of Xcode, make sure to build the package before publishing to git. This will reveal errors that you otherwise wouldn't have noticed and will save you tons of debugging time

    在Xcode内部,请确保在发布到git之前先构建包。 这将显示您可能不会注意到的错误,并节省大量调试时间
  • Test locally before pushing to git (I’ll go into how to do this next)

    在推送到git之前先进行本地测试(接下来我将介绍如何执行此操作)

部署选项 (Deployment Options)

本地测试 (Local Testing)

Before publishing your package anywhere, you definitely want to make sure to integrate and test it with an Xcode project locally.

在将软件包发布到任何地方之前,您肯定要确保与本地Xcode项目集成并对其进行测试。

Doing this is pretty simple. First you’ll want to make sure your files are being tracked by git and you have the most recent changes committed. Once that’s taken care of, you’re ready to import the package into an Xcode project for testing.

这样做很简单。 首先,您需要确保git跟踪了您的文件,并且您已提交了最新的更改。 处理完之后,您就可以将包导入Xcode项目中进行测试了。

Find and copy the absolute path to your package, then go to File > Swift Packages > Add Package Dependency…, and enter file://{path} as the url. At this point you can choose what branch, commit id, or version you want to use. Continue and it will automatically install and link your project as well as any dependencies.

找到并复制到软件包的绝对路径,然后转到“ 文件”>“ Swift软件包”>“添加软件包依赖性...”,然后输入file:// {path}作为URL。 此时,您可以选择要使用的分支,提交ID或版本。 继续,它将自动安装并链接您的项目以及所有依赖项。

If you make any changes to your code, make sure to commit them then update the package within Xcode. You can do this by navigating to File > Swift > Packages > Update To Latest Package Versions.

如果您对代码进行任何更改,请确保提交它们,然后在Xcode中更新程序包。 您可以通过导航至“ 文件”>“ Swift”>“软件包”>“更新为最新软件包版本”来执行此操作。

发布到SPM(快速包管理器) (Publishing to SPM (Swift Package Manager))

Once you’re ready to publish to SPM, the process is almost as easy as it was to test locally. Simply push your local commits to GitHub (or any other code repo) and you can follow the process above just replacing the local filepath with the repo URL.

一旦准备好发布到SPM,该过程几乎就像在本地进行测试一样容易。 只需将本地提交推送到GitHub(或任何其他代码存储库),就可以按照上述过程进行操作,只是将本地文件路径替换为存储库URL。

发布到CocoaPods (Publishing to CocoaPods)

Another very popular package manager is CocoaPods. Publishing to CocoaPods is a bit more complicated than SPM, but if you’re looking to make it as easy as possible for other develops to integrate your package I would definitely recommend looking into it.

另一个非常流行的软件包管理器是CocoaPods。 发布到CocoaPods的过程比SPM复杂得多,但是如果您想使其与其他开发人员尽可能轻松地集成您的软件包,我绝对建议您对其进行研究。

Heres a link to the documentation on the CocoaPods website if you’re interested:

如果您对此感兴趣,可以在此处链接到CocoaPods网站上的文档:

有关其他信息,请查看: (For additional info, check out:)

翻译自: https://medium.com/swlh/building-an-sdk-for-ios-in-swift-688d13f0a6fb

swift 封装sdk

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值