一个精简的、具有一定主观性的库,用于检索和展示您希望在您的应用中展示的其他应用。对同一开发者进行应用交叉推广非常有用。
将 https://github.com/marionauta/apps-showcase-swift
添加到您的 Swift Package Manager 依赖项中。不支持其他依赖管理器。
AppsShowcase
需要一个托管在某个地方的 .json
文件,其结构如下所示:
[
{
"id": "string", // An unique string, usually the bundle id.
"name": "string",
"tagline": "string",
"ios": "string", // A valid url to the app.
"beta": true // Optional. Marks the app as beta if true.
}
]
然后在您的代码中:
import AppsShowcase
func load() async {
let showcase = AppsShowcase(url: URL(string: "https://example.com/apps.json")!)
switch await showcase.retrieve() {
case let .success(apps):
print(apps)
case let .failure(error):
print(error.localizedDescription)
}
}
默认情况下,AppsShowcase
会省略与当前运行的应用具有相同 bundle id 的应用。可以通过以下方式禁用此行为:
let showcase = AppsShowcase(url: URL(string: "...")!, excludingId: nil)
您可以随意展示 ShowcasedApp
对象。 提供了两个基本的视图 ShowcasedAppRow
和 ShowcasedAppLink
,可以直接使用。
import SwiftUI
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
struct AppList: View {
let apps: [ShowcasedApp]
var body: some View {
ForEach(apps) { app in
Link(destination: app.url) {
ShowcasedAppRow(app: app)
}
}
ForEach(apps) { app in
ShowcasedAppLink(app: app) // Uses `Link` for you
}
}
}
此外,还提供了一个 ShowcasedAppsSection
视图,用于处理获取和展示应用列表。
import SwiftUI
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
struct AppList: View {
@State private var isLoading: Bool = false
var body: some View {
// Simple
ShowcasedAppsSection("My other apps", url: URL(string: "...")!)
// To report the loading status:
ShowcasedAppsSection("My other apps", url: URL(string: "...")!, isLoading: $isLoading)
}
}