gpt4 book ai didi

swift - iOS 14 WidgetKit 意图

转载 作者:行者123 更新时间:2023-12-04 14:10:24 29 4
gpt4 key购买 nike

我有一个设置了 .intentdefinition 文件的 WidgetKit ,我可以在运行我的 WidgetKit 时从我的枚举中进行选择,但我不确定如何在代码中使用这些信息。

我希望能够根据用户在 WidgetKit 设置中选择的内容在 getTimeline 中运行不同的代码。

如果有人能提供帮助,那将不胜感激。谢谢!

最佳答案

要创建一个可配置 WidgetKit ,您需要使用IntentConfigurationIntentTimelineProvider。这将允许您访问稍后可以在 getTimeline 函数中使用的意图参数。

  1. 在您的 Widget 中,您需要使用 IntentConfiguration(而不是 StaticConfiguration)并选择要使用的意图(intent 参数):<
struct IntentWidget: Widget {
let kind: String = "IntentWidget"

var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: CustomIntent.self, provider: Provider()) { entry in
WidgetEntryView(entry: entry)
}
.configurationDisplayName("Intent Widget")
.description("Intent Widget")
}
}
  1. 使您的提供者符合 IntentTimelineProvider(而不是 TimelineProvider)。然后您就可以在 getSnapshotgetTimeline 方法中使用您的意图:
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date())
}

func getSnapshot(for configuration: CustomIntent, in context: Context, completion: @escaping (SimpleEntry) -> Void) {
completion(SimpleEntry(date: Date()))
}

func getTimeline(for configuration: CustomIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
// here you can access your intent configuration
// let selectedItem = configuration.selectedItem
...
completion(timeline)
}
}

有用的链接:


这是一个GitHub repository使用不同的 Widget 示例,包括 Intent Widget。

关于swift - iOS 14 WidgetKit 意图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64959319/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com