小部件未更新
我有一个应用程序和一个小部件,我希望每天午夜后更新它们。应用程序正在更新,但小部件没有安装。尝试了时间轴的不同方法,但它不起作用。在模拟器中它工作正常,但当我在手机上运行该应用程序时(通过 TestFlight)则不然。你们中有人能帮帮我吗!
小部件代码下方:
...
import WidgetKit
import SwiftUI
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>) -> Void) {
let currentDate = Date()
let entries = [
SimpleEntry(date: currentDate)
]
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
}
struct AhackadayWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack {
Spacer()
Text("Your Hack of the Day")
.font(.footnote)
Divider()
.padding(.horizontal)
.foregroundColor(Color.gray)
Spacer()
Text(mijnPlaatje.mijnKop())
.font(.headline)
.multilineTextAlignment(.center)
.padding(.horizontal)
Image(mijnPlaatje.WidgetPicture())
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(RoundedRectangle(cornerRadius: 15))
.padding(.horizontal)
Spacer()
}
.widgetURL(URL(string: "myapp://link"))
}
}
@main
struct AhackadayWidget: Widget {
let kind: String = "AhackadayWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
AhackadayWidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct AhackadayWidget_Previews: PreviewProvider {
static var previews: some View {
AhackadayWidgetEntryView(entry: SimpleEntry(date: Date()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
...
应用程序本身中的代码,用于调用下面的时间线更新。
应用程序本身会执行 以下操作:每天晚上更新。所以 TimeChangeNotification 工作正常。但是,该小部件未更新。
.onReceive(NotificationCenter.default.publisher(
for: UIApplication.significantTimeChangeNotification)) { _ in
ContentView.displayDate = Date()
curDate = myPict.myPicture()
WidgetCenter.shared.reloadAllTimelines()
I have an app and a widget that I would like to be updated every day after midnight. The app is updating, but the widget ins't. Tried different approaches with the Timeline, but it is not working. In the simulator it is working fine, but not when I run the app on my phone (via TestFlight). Can one of you help me out!
Below the code for the widget:
...
import WidgetKit
import SwiftUI
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>) -> Void) {
let currentDate = Date()
let entries = [
SimpleEntry(date: currentDate)
]
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
}
struct AhackadayWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack {
Spacer()
Text("Your Hack of the Day")
.font(.footnote)
Divider()
.padding(.horizontal)
.foregroundColor(Color.gray)
Spacer()
Text(mijnPlaatje.mijnKop())
.font(.headline)
.multilineTextAlignment(.center)
.padding(.horizontal)
Image(mijnPlaatje.WidgetPicture())
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(RoundedRectangle(cornerRadius: 15))
.padding(.horizontal)
Spacer()
}
.widgetURL(URL(string: "myapp://link"))
}
}
@main
struct AhackadayWidget: Widget {
let kind: String = "AhackadayWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
AhackadayWidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct AhackadayWidget_Previews: PreviewProvider {
static var previews: some View {
AhackadayWidgetEntryView(entry: SimpleEntry(date: Date()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
...
The code in the app itself to call for an update of the timeline below.
The app itself does the update every night. So the TimeChangeNotification is working correctie. However, the widget is not updated.
.onReceive(NotificationCenter.default.publisher(
for: UIApplication.significantTimeChangeNotification)) { _ in
ContentView.displayDate = Date()
curDate = myPict.myPicture()
WidgetCenter.shared.reloadAllTimelines()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您
正在告诉 IOS 尽快重新加载您的小部件。即使没有新的数据可用。由于 IOS 正在管理 Widget 更新的时间点,因此它将开始大约每 5 - 15 分钟更新一次。但随着时间的推移,这将开始需要更多。
TLDR:您很可能超出了重新加载的预算。
尝试:
这样,您的时间线只会在您的应用程序希望重新加载时重新加载。
确保您
接到电话。
参考:
https://developer.apple.com/documentation/ widgetkit/keeping-a-widget-up-to-date
编辑:
在调试器中进行的更新不会计入您的预算。
With
you are telling IOS to reload of your widget as soon as possible. Even if no new data is available. As IOS is managing the point in Time when the Widget gets updated it will start to update about every 5 - 15 minutes at the beginning. But it will start to take much more as time passes.
TLDR: You are most probably out of budget for reloads.
Try:
With this your timeline will only reload when your app wants it to reload.
Make sure your
gets called.
Reference:
https://developer.apple.com/documentation/widgetkit/keeping-a-widget-up-to-date
Edit:
Updates while you are in the debugger do not count against your budget.