IntentTimelineProvider Protocol

IntentTimelineProvider

一个通知WidgetKit何时更新用户可配置widget的显示的类型

定义

  1. protocol IntentTimelineProvider

概述

Intent时间线提供者执行与TimelineProvider相同的功能,但它也将用户配置的细节纳入时间线条目中。 例如,在一个显示用户选择的游戏角色的健康状态的小组件中,提供者收到了一个指定要显示的角色的自定义意图。在Xcode项目中,您可以定义一个自定义的SiriKit意图定义文件。意图定义可以包括角色的详细信息,如它的名字、头像、战略联盟等。

IntentTimelineProvider-IntentDefinition@2x

Xcode生成以下INIntent自定义意图。

  1. public class SelectCharacterIntent: INIntent {
  2. @NSManaged public var characterName: String?
  3. @NSManaged public var avatar: String?
  4. @NSManaged public var alliances: [String]?
  5. @NSManaged public var healthLevel: NSNumber?
  6. }

因为用户可以添加一个特定widget的多个实例,你的提供者需要一种方法来区分WidgetKit询问的是哪个实例。

当WidgetKit调用snapshot(for:with:completion:)或timeline(for:with:completion:)时,它会传递一个您的自定义INIntent的实例,并配置了用户选择的详细信息。

游戏widget提供者访问intent的属性并将其包含在TimelineEntry中。然后,WidgetKit调用widget配置的内容闭合,传递时间线条目以允许视图访问用户配置的属性。例如,提供者可以实现一个TimelineEntry,其属性与自定义意图中的属性相对应。「译者注:例如自动生成的代码中的 SampleEntry

  1. struct CharacterDetailEntry: TimelineEntry {
  2. var date: Date
  3. var name: String?
  4. var avatar: String?
  5. var alliances: [String]?
  6. var healthLevel: Double?
  7. }

要生成快照,游戏小组件提供者使用来自意图的属性初始化角色细节条目。

  1. struct CharacterDetailProvider: IntentTimelineProvider {
  2. func snapshot(for configuration: SelectCharacterIntent, with context: Context, completion: @escaping (CharacterDetailEntry) -> ()) {
  3. let entry = CharacterDetailEntry(
  4. date: Date(),
  5. name: configuration.characterName,
  6. avatar: configuration.avatar,
  7. alliances: configuration.alliances,
  8. healthLevel: configuration.healthLevel?.doubleValue
  9. )
  10. completion(entry)
  11. }
  12. }

Topics「参照10. TimelineProvider 此处不再赘述」

Generating Timelines

func snapshot(for: Self.Intent, with: Self.Context, completion: (Self.Entry) -> ())?language=swift)

Provides a timeline entry representing the current time and state of a widget.

Required.

func timeline(for: Self.Intent, with: Self.Context, completion: (Timeline) -> ())?language=swift)

Provides an array of timeline entries for the current time and, optionally, any future times to update a widget.

Required.

associatedtype Entry : TimelineEntry

Required.

associatedtype Intent : INIntent

Required.

Type Aliases

typealias Context

See Also

Related Documentation

class INIntent

A request to fulfill in your app or Intents extension.