Hello!
I made a lock screen widget that is much-needed,
However, it does not appear on the lock screen of the actual iOS.
I tried changing the Extension, but that does not appear to work. Here is my code:
import WidgetKit
import SwiftUI
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SecondsEntry {
SecondsEntry(date: Date())
}
func getSnapshot(in context: Context, completion: @escaping (SecondsEntry) -> ()) {
let entry = SecondsEntry(date: Date())
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SecondsEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for secondOffset in 0 ..< 60 {
let entryDate = Calendar.current.date(byAdding: .second, value: secondOffset, to: currentDate)!
let entry = SecondsEntry(date: entryDate)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct SecondsEntry: TimelineEntry {
let date: Date
}
struct SecondsEntryView : View {
@Environment(\.widgetFamily) var widgetFamily
var entry: Provider.Entry
var body: some View {
switch widgetFamily {
case .accessoryCircular:
Gauge(value: 1) {
Text(entry.date.secondDisplayFormat)
.font(.largeTitle)
}
.gaugeStyle(.accessoryCircularCapacity)
case .accessoryRectangular:
HStack {
Text(entry.date.secondDisplayFormat)
.font(.title)
Text("Seconds")
.font(.headline)
}
case .accessoryInline:
Text("● " + entry.date.secondDisplayFormat + " Seconds")
.font(.largeTitle)
default:
Text("Error loading...")
}
}
}
struct Seconds: Widget {
let kind: String = "Seconds"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
SecondsEntryView(entry: entry)
}
.configurationDisplayName("Seconds")
.description("You will be able to track seconds, directly on your lock screen")
.supportedFamilies([.accessoryInline, .accessoryCircular, .accessoryRectangular])
}
}
struct Seconds_Previews: PreviewProvider {
static var previews: some View {
SecondsEntryView(entry: SecondsEntry(date: Date()))
.previewContext(WidgetPreviewContext(family: .accessoryInline))
.previewDisplayName("Inline")
SecondsEntryView(entry: SecondsEntry(date: Date()))
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
.previewDisplayName("Circular")
SecondsEntryView(entry: SecondsEntry(date: Date()))
.previewContext(WidgetPreviewContext(family: .accessoryRectangular))
.previewDisplayName("Rectangular")
}
}
extension Date {
var secondDisplayFormat: String {
self.formatted(.dateTime.second())
}
}