My complication code is very similar to that found in the Migrating ClockKit complications to WidgetKit tutorial. Except, I am only interested in displaying the app icon as the complication.
I just cannot get any 'custom' images to display in the simulator, nor on a Watch. Image(systemImage: "")
work fine, however.
Does the image need to be re-sized to a specific resolution? The original image is 1024×1024, I have tried re-sizing this down to multiple smaller resolutions with no success.
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)
}
func getTimeline(for configuration: ConfigurationIntent, 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, configuration: configuration)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
func recommendations() -> [IntentRecommendation<ConfigurationIntent>] {
return [
IntentRecommendation(intent: ConfigurationIntent(), description: "Treadmill Assistant")
]
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
}
struct Tread_Cadence_ComplicationEntryView : View {
var entry: Provider.Entry
var body: some View {
Image("AppIcon")
//.resizable()
//.frame(width: 50, height: 50)
//.scaledToFill()
}
}
@main
struct Tread_Cadence_Complication: Widget {
let kind: String = "Tread_Cadence_Complication"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
Tread_Cadence_ComplicationEntryView(entry: entry)
}
.configurationDisplayName("Treadmill Assistant")
.description("Opens Treadmill Assistant.")
}
}
struct Tread_Cadence_Complication_Previews: PreviewProvider {
static var previews: some View {
Tread_Cadence_ComplicationEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .accessoryRectangular))
}
}