DeepLink

使用 Swift 宏创建和使用类型安全的 deep links。

@DeepLink(generateInitWithURL: true)
public struct SearchDeepLink: DeepLink {
    public static let scheme = "example"

    @Host
    private let host = "search"

    @PathItem
    public var category = "all"

    @QueryItem(name: "q")
    public var query: String
}

// DeepLink will add `url` property and `init(url:)`

SearchDeepLink.scheme // "example"
SearchDeepLink(query: "hello world").url.absoluteString // "example://search/all?q=hello%20world"

let deepLink = SearchDeepLink(url: URL(string: "example://search/tv?q=still%20up")!)!
deepLink.category // "tv"
deepLink.query // "still up"

支持的应用

想要向项目添加应用?请开启一个 pull request 或 提交一个 issue

类型灵活性

DeepLink 并非严格限制其类型,而是依赖字符串插值来构建 URL。这允许属性是任何符合 CustomStringConvertible 协议的类型,例如 Int、枚举和自定义类型。

如果属性是可选的,则仅当它为非 nil 时才用于构建 URL。这对于可选的查询项特别有用。

这也扩展到使用 URL 初始化 deep link -- 只要类型符合 LosslessStringConvertible 协议即可。

状态

我目前将 DeepLink 归类为 beta 包。我没有意识到实现方面有任何问题,但在我对 API 更满意之前,我将暂缓发布 1.0 版本。API 更改将尽可能包含 @available(*, deprecated, renamed: "<new-name>"),因此更新应该不会很麻烦。

1.0 版本的最低要求

我将一些事项归类为 1.0 版本的必需项

1.0 版本之后

在未来的版本中可以做很多事情,例如

为什么

这个想法源于我看到了 Mastodon 上 Callsheet 的 URL scheme,这让我思考了我是如何在我的一个 未发布的项目 中添加 Ivory URL scheme 的。

然后我认为这将是一个使用 Swift 宏的有趣项目,那时我已经被自己吸引住了。我也认为拥有一个可用于各种应用的 URL scheme 仓库很有用。

生成链接

Swift REPL 可用于在终端中生成链接。这对于测试其他应用如何工作很有用,或者如果您正在为新应用添加支持,以测试生成的字符串是否有效(这不能替代单元测试 😉)。

# Assumes you're in the package's root
$ swift run --repl
Welcome to Apple Swift version 5.9 (swiftlang-5.9.0.128.2 clang-1500.0.40.1).
Type :help for assistance.
  1> import CallsheetDeepLink
  2> ActivateInput().url
$R0: Foundation.URL = "callsheet://activateInput"