简体   繁体   English

iOS 14 小部件未更新

[英]iOS 14 Widget not update

I'm trying to build an IOS 14 Widget, that updates every two minutes.我正在尝试构建一个 IOS 14 小部件,每两分钟更新一次。 This is the widget code:这是小部件代码:

import WidgetKit
import SwiftUI
import Intents

struct SimpleEntry: TimelineEntry {
    let date: Date
    let configuration: ConfigurationIntent
}

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>) -> ()) {
        let entry1 = SimpleEntry(date: Date(), configuration: configuration)

        let timeline = Timeline(entries: [entry1], policy: .after(Date().addingTimeInterval(2*60.0)))
        print("timeline: \(timeline)")
        completion(timeline)
    }
}

struct TeamWidgetEntryView : View {
    @Environment(\.widgetFamily) var family
    var entry: Provider.Entry

    @ViewBuilder
    var body: some View {

        switch family {
            case .systemSmall:
                SmallView(date: Date())
            case .systemMedium:
                MediumView()
            case .systemLarge:
                LargeView()
            default:
                Text("Some other WidgetFamily in the future.")
        }
    }
}

@main
struct TeamWidget: Widget {
    let kind: String = "TeamWidget"

    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
            TeamWidgetEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
        .supportedFamilies([.systemSmall,.systemMedium,.systemLarge])
    }
}

This is the SmallView class:这是 SmallView class:

struct SmallView: View {
    var date: Date

    static let taskDateFormat: DateFormatter = {
        let formatter = DateFormatter()
        formatter.timeStyle = .medium
        return formatter
    }()
    
    var body: some View {
        VStack {
            Text("Small View")
            Text("\(Date(), formatter: Self.taskDateFormat)")
        }
    }
}

I want that the Widget will update every 2 minutes but it's not happening, any idea what is the problem?我希望小部件每 2 分钟更新一次,但它没有发生,知道有什么问题吗?

I was also facing the same problem and tried every update time from 10 seconds to 10 minutes and it seems that it starts updating the content only from 5 minutes我也面临同样的问题,并尝试从 10 秒到 10 分钟的每次更新时间,似乎它仅从 5 分钟开始更新内容

this is what I use to update my widget every minute这就是我用来每分钟更新我的小部件的东西

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))
            }
            
            let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
            completion(timeline)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM