[英]iOS 14 widget intents
我有一个设置了.intentdefinition
文件的小部件,我可以在运行小部件时从我的枚举中进行选择,但我不确定如何在代码中使用此信息。
我想要做的是根据用户在小部件设置中选择的内容在getTimeline
运行不同的代码。
如果有人可以提供帮助,那将不胜感激。 谢谢!
要创建可配置的小部件,您需要使用IntentConfiguration
和IntentTimelineProvider
。 这将允许您访问您稍后可以在getTimeline
函数中使用的意图参数。
IntentConfiguration
(而不是StaticConfiguration
)并选择要使用的intent
( 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")
}
}
IntentTimelineProvider
(而不是TimelineProvider
)。 然后你就可以在getSnapshot
和getTimeline
方法中使用你的意图: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 存储库,其中包含不同的 Widget 示例,包括 Intent Widget。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.