Post

Replies

Boosts

Views

Created

How to display data from HealthKit and internet in WidgetKit
I'm developing a widget app that coins multiple functional widgets. The problem is fetching data. I want to fetch data from HealthKit to display step counts and fetch weather data. I'm using just one Widget object. I show different widgets by changing the View objects in them according to the widget type which is a String property. I can fetch data from EventKit for calendar events, but reminders, health information, and internet fetching is not working on my code. So where am I doing wrong? My widget provider is looking like that: func getTimeline(for configuration: WidgetConfigurationIntentIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) { if let widget = lookUpWidget(for: configuration) { let currentDate = Date() let midnight = Calendar.current.startOfDay(for: currentDate) let nextMinute = Calendar.current.date(byAdding: .minute, value: 1, to: midnight)! let type = widget.type switch type { case "Quotes": let entry = MainEntry(date: Date(), type: widget.type!, backgroundImageData: widget.backgroundImage, backgroundHexColor: widget.backgroundHexColor, themeHexColor: widget.themeHexColor, fontName: widget.fontName, text: widget.text) let timeline = Timeline(entries: [entry], policy: .after(nextMinute)) completion(timeline) case "Daily Forecast": let entry = MainEntry(date: Date(), type: widget.type!, weatherInfo: loadWeatherInfo(), backgroundImageData: widget.backgroundImage, backgroundHexColor: widget.backgroundHexColor, themeHexColor: widget.themeHexColor, fontName: widget.fontName) let timeline = Timeline(entries: [entry], policy: .after(nextMinute)) completion(timeline) case "Calendar": let entry = MainEntry(date: Date(), type: widget.type!, backgroundImageData: widget.backgroundImage, backgroundHexColor: widget.backgroundHexColor, themeHexColor: widget.themeHexColor, fontName: widget.fontName, calendarEventTitles: loadCalendarEventTitles()) let timeline = Timeline(entries: [entry], policy: .after(nextMinute)) completion(timeline) case "Countdown": let entry = MainEntry(date: Date(), type: widget.type!, backgroundImageData: widget.backgroundImage, backgroundHexColor: widget.backgroundHexColor, themeHexColor: widget.themeHexColor, fontName: widget.fontName, text: widget.text, countdownDate: widget.countdownDate) let timeline = Timeline(entries: [entry], policy: .after(nextMinute)) completion(timeline) case "Reminders": let entry = MainEntry(date: Date(), type: widget.type!, backgroundImageData: widget.backgroundImage, backgroundHexColor: widget.backgroundHexColor, themeHexColor: widget.themeHexColor, fontName: widget.fontName, reminderTitles: loadReminderTitles()) let timeline = Timeline(entries: [entry], policy: .after(nextMinute)) completion(timeline) case "Battery": let entry = MainEntry(date: Date(), type: widget.type!, batteryLevel: Int(loadBatteryLevel()), backgroundImageData: widget.backgroundImage, backgroundHexColor: widget.backgroundHexColor, themeHexColor: widget.themeHexColor, fontName: widget.fontName) let timeline = Timeline(entries: [entry], policy: .after(nextMinute)) completion(timeline) case "Clock": let entry = MainEntry(date: Date(), type: widget.type!, backgroundImageData: widget.backgroundImage, backgroundHexColor: widget.backgroundHexColor, themeHexColor: widget.themeHexColor, fontName: widget.fontName) let timeline = Timeline(entries: [entry], policy: .after(nextMinute)) completion(timeline) case "Steps": let entry = MainEntry(date: Date(), type: widget.type!, backgroundImageData: widget.backgroundImage, backgroundHexColor: widget.backgroundHexColor, themeHexColor: widget.themeHexColor, fontName: widget.fontName, stepCount: loadStepCount()) let timeline = Timeline(entries: [entry], policy: .after(nextMinute)) completion(timeline) case "Photos": let entry = MainEntry(date: Date(), type: widget.type!, backgroundImageData: widget.backgroundImage, backgroundHexColor: widget.backgroundHexColor, fontName: widget.fontName) let timeline = Timeline(entries: [entry], policy: .after(nextMinute)) completion(timeline) default: let entry = MainEntry(date: Date(), type: "Nothing") let timeline = Timeline(entries: [entry], policy: .atEnd) completion(timeline) } } else { let entry = MainEntry(date: Date(), type: "Nothing") let timeline = Timeline(entries: [entry], policy: .atEnd) completion(timeline) } }
0
0
623
Aug ’21
Making dynamic widgets in WidgetBundle
I want to make an application where the user can add as many Widgets as they want. So I need to define dynamic widgets in WidgetBundle. I tried this code the below but error appears: @WidgetBundleBuilder     var body: some Widget {         List {             ForEach(0..<2) { _ in                 CalendarWidget()             }         }     } Is there any way to do dynamic widgets?
0
0
700
Jun ’21