从主应用程序目标 SwiftUI 访问 WidgetKit IntentTimelineProvider
Accessing WidgetKit IntentTimelineProvider from main app target SwiftUI
我正在尝试从主应用程序访问 WidgetExtension
的 IntentTimelineProvider
。我这样做是为了从 IntentTimelineProvider
中获取日期 属性 并在主应用程序目标的视图中使用它,正如您从下面的代码中看到的那样。
我已将文件(主应用程序目标中的文件和小部件扩展目标中的文件)的目标成员设置为应用程序和小部件。
插件扩展
import WidgetKit
import SwiftUI
import Intents
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent())
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration)
completion(entry)
}
//build time error here: "Reference to invalid associated type 'Entry' of type 'Provider'" here...
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries = [SimpleEntry]()
let currentDate = Date()
let midnight = Calendar.current.startOfDay(for: currentDate)
let nextMidnight = Calendar.current.date(byAdding: .day, value: 1, to: midnight)!
for offset in 0 ..< 60 * 24 {
let entryDate = Calendar.current.date(byAdding: .minute, value: offset, to: midnight)!
entries.append(SimpleEntry(date: entryDate, configuration: configuration))
}
let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
}
struct TimeWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
NotingView(entry: entry)
}
}
@main
struct TimeWidget: Widget {
let kind: String = "TimeWidget"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
TimeWidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct TimeWidget_Previews: PreviewProvider {
static var previews: some View {
TimeWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
主应用程序
import SwiftUI
struct SampleView: View {
var entry : Provider.Entry
var body: some View {
Text(entry.date, style: .time)
}
}
我目前遇到构建时错误:
“
引用类型 'Provider' 的无效关联类型 'Entry'
“
在 getTimeline()
函数中。
在您的主应用中,使用 SimpleEntry
而不是 Provider.Entry
。后者引用了你实现的 TimelineProvider
协议的关联类型,但无论出于何种原因,应用程序都无法推断出类型(也许 Swift-savvy 比我解释得更多)。但是你不需要推断它,只需要给它实际的类型:SimpleEntry
.
struct SampleView: View {
var entry : SimpleEntry
var body: some View {
Text(entry.date, style: .time)
}
}
我正在尝试从主应用程序访问 WidgetExtension
的 IntentTimelineProvider
。我这样做是为了从 IntentTimelineProvider
中获取日期 属性 并在主应用程序目标的视图中使用它,正如您从下面的代码中看到的那样。
我已将文件(主应用程序目标中的文件和小部件扩展目标中的文件)的目标成员设置为应用程序和小部件。
插件扩展
import WidgetKit
import SwiftUI
import Intents
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent())
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration)
completion(entry)
}
//build time error here: "Reference to invalid associated type 'Entry' of type 'Provider'" here...
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries = [SimpleEntry]()
let currentDate = Date()
let midnight = Calendar.current.startOfDay(for: currentDate)
let nextMidnight = Calendar.current.date(byAdding: .day, value: 1, to: midnight)!
for offset in 0 ..< 60 * 24 {
let entryDate = Calendar.current.date(byAdding: .minute, value: offset, to: midnight)!
entries.append(SimpleEntry(date: entryDate, configuration: configuration))
}
let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
}
struct TimeWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
NotingView(entry: entry)
}
}
@main
struct TimeWidget: Widget {
let kind: String = "TimeWidget"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
TimeWidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct TimeWidget_Previews: PreviewProvider {
static var previews: some View {
TimeWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
主应用程序
import SwiftUI
struct SampleView: View {
var entry : Provider.Entry
var body: some View {
Text(entry.date, style: .time)
}
}
我目前遇到构建时错误:
“
引用类型 'Provider' 的无效关联类型 'Entry'
“
在 getTimeline()
函数中。
在您的主应用中,使用 SimpleEntry
而不是 Provider.Entry
。后者引用了你实现的 TimelineProvider
协议的关联类型,但无论出于何种原因,应用程序都无法推断出类型(也许 Swift-savvy 比我解释得更多)。但是你不需要推断它,只需要给它实际的类型:SimpleEntry
.
struct SampleView: View {
var entry : SimpleEntry
var body: some View {
Text(entry.date, style: .time)
}
}