I want to show lottie animation in iOS WidgetKit. I use this project https://github.com/airbnb/lottie-ios. It works well in APP. But same code not work in WidgetKit.
The code in APP is as bellow, which works well in APP:
struct widgetTestyView : View {
var body: some View {
VStack() {
//Text("\(entry.date)")
LottieView(animation: LottieAnimation.named("Samples/9squares_AlBoardman")!)
}
}
}
But same code did not work in WidgetKit, the code is as bellow:
//
// widget.swift
// widget
//
import WidgetKit
import SwiftUI
import Lottie
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date())
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date())
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
}
struct widgetEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack() {
//Text("\(entry.date)")
LottieView(animation: LottieAnimation.named("Samples/9squares_AlBoardman")!)
}
}
}
@main
struct widget: Widget {
let kind: String = "widget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
widgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct widget_Previews: PreviewProvider {
static var previews: some View {
widgetEntryView(entry: SimpleEntry(date: Date()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
Is it possible to show lottie in WidgetKit?