IntentConfiguration Structure

IntentConfiguration

描述小组件内容的对象,该对象使用自定义意图定义来提供用户可配置的选项。


定义

  1. struct IntentConfiguration<Intent, Content> where Intent : INIntent, Content : View

当Intent继承INIntent且Content符合View时可用。

概述

下面的例子显示了一个显示所选角色详细信息的游戏部件的配置。

  1. struct CharacterDetailWidget: Widget {
  2. var body: some WidgetConfiguration {
  3. IntentConfiguration(
  4. kind: "com.mygame.character-detail", // 唯一标识符
  5. intent: SelectCharacterIntent.self, // 自定义的intent文件
  6. provider: CharacterDetailProvider(), // 时间线Provider
  7. placeholder: CharacterPlaceholderView() // Placeholder
  8. ) { entry in
  9. CharacterDetailView(entry: entry) // Body
  10. }
  11. .configurationDisplayName("Character Details")
  12. .description("Displays a character's health and other details")
  13. .supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
  14. }
  15. }

每个小组件都有一个唯一的kind,一个您选择的字符串。当你使用WidgetCenter重新加载它的时间线时,你可以使用这个字符串来识别你的widget。

intent是一个自定义的 SiriKit 意图定义,包含用户可编辑的参数。

时间线provider是一个决定刷新您的小组件的时间线的对象。提供更新您的小组件的未来日期可以让系统优化刷新过程。

要首次显示您的小组件,或在锁屏上显示它,您需要提供一个占位符视图(placeholder),它是一个没有特定内容的通用视觉表示。占位符视图类似于 watchOS 中复杂功能的占位符资产,只不过它是作为 SwiftUI 视图提供的。

内容闭包包含WidgetKit渲染小部件所需的SwiftUI视图。当WidgetKit调用内容闭包时,它会传递一个由widget提供者的snapshot(for:with:completion:)或timeline(for:with:completion:)方法创建的时间线条目。 修改器让你指定你的widget支持的规格,以及当用户添加或编辑他们的widget时显示的细节。

Topics

参照 widgetConfiguration