从主应用程序目标 SwiftUI 访问 WidgetKit IntentTimelineProvider
Posted
技术标签:
【中文标题】从主应用程序目标 SwiftUI 访问 WidgetKit IntentTimelineProvider【英文标题】:Accessing WidgetKit IntentTimelineProvider from main app target SwiftUI 【发布时间】:2021-11-22 20:37:10 【问题描述】:我正在尝试从主应用访问 IntentTimelineProvider
或 WidgetExtension
。我这样做是为了从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()
函数。
【问题讨论】:
【参考方案1】:在您的主应用程序中,使用SimpleEntry
而不是Provider.Entry
。后者引用了 TimelineProvider
协议实现的相关类型,但无论出于何种原因,应用程序都无法推断出类型(也许有人比我更精通 Swift)。但是你不需要推断它,只需给它实际的类型:SimpleEntry
。
struct SampleView: View
var entry : SimpleEntry
var body: some View
Text(entry.date, style: .time)
【讨论】:
以上是关于从主应用程序目标 SwiftUI 访问 WidgetKit IntentTimelineProvider的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI:如何从 Siri Intent 添加 CoreData 记录
使用 SwiftUI @Binding 预览崩溃:与应用程序的通信中断