Announcement: Join the Conversation
Find discussions by searching for keywords or tags.

View all tags >
Learn more about the forums >

WidgetKit

Show relevant, glanceable content from your app on the iOS Home screen or in macOS Notification Center.

WidgetKit Documentation

Posts under WidgetKit tag

163 results found
Post marked as unsolved
4 Views

WidgetKit Mac Catalyst app

Hello, Configurable widget does not work in Catalyst app. Configurable widget works fine in iPhone, but when tried to run on Mac for catalyst, it does not work, gives error. Tested app in iPad and it works fine. Does it require additional settings? Thanks Hiren
Asked
Last updated .
Post marked as unsolved
2 Views

Registering custom fonts from a framework in a widget extension

I want a widget to use the same custom fonts that the containing app uses. I do not want multiple copies of the font files in the app bundle. I put the font files into a framework shared by the app and the widget extension. I have code to register the fonts from the framework bundle using CTFontManagerRegisterGraphicsFont. That works from the app, but it does not work from the widget extension because it requires UIKit. (At least I think that's why it doesn't seem to work from my widget extension.) Widgets cannot run UIKit code. Is there a way to programmatically register the fonts in a widget? If not is there a way I can make a UIAppFonts Info.plist entry point to a framework?
Asked
Last updated .
Post marked as solved
65 Views

cannot find type 'ConfigurationIntent' in scope

In the OC project, iOS14 WidgetKit target was added, but consistent error Cannot find type "ConfigurationIntent" in scope, type 'Provider' does not conform to protocol 'IntentTimelineProvider' What is strange is that after adding a new OC project, the compilation is successful, Tool: Xcode12-beta & Simulator How to get access to old projects?
Asked
by zhangkk.
Last updated .
Post marked as unsolved
34 Views

How to use Timer in the new Widget API?

I'm building a new widget for one of my apps, and the new Text initializer init(_:style:) is not enough for me. I need to have some control over which NSCalendar.Units are displayed, so I'm trying to use DateComponentsFormatter here. I have to use my own Timer because of that, and here's my code. struct TimerText: View {     let date: Date     let allDay: Bool     let unitFlags: NSCalendar.Unit     private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()     @State var duration: String?     init(date: Date, allDay: Bool, unitFlags: NSCalendar.Unit) {         self.date = date         self.allDay = allDay         self.unitFlags = unitFlags         updateDuration()     }     func updateDuration() {         duration = systemDurationString(date, allDay: allDay, unitFlags: unitFlags)     }     var body: some View {         Text(duration ?? "—")             .font(.system(.title, design: .monospaced))             .onReceive(timer) { _ in                 updateDuration()             }     } } systemDurationString function uses DateComponentsFormatter inside and returns the formatted string. Unfortunately, onReceive callback never gets called. I thought this might be a problem with RunLoop that I've chosen, but it's seems like .current doesn't work either.
Asked
by randexdev.
Last updated .
Post marked as unsolved
181 Views

WidgetKit background like the system battery widget

Hello, is it possible to configure our widgets with a background like the inbuilt system battery widget? It looks like it's using a UIVisualEffectView as it's background which looks fantastic against the user's background. Especially as it transitions for dark mode. I've been through all the docs and designing for WidgetKit but can't figure out how to have anything but a fully opaque background. Setting the background to transparent on my SwiftUI view results in white showing as the background. Thanks, Greg
Asked
by gspiers.
Last updated .
Post marked as unsolved
21 Views

iOS 14 widgets: Content update frequency and latency limits and best practices

With regards to the new iOS 14 widgets implemented with WidgetKit: For a widget that needs to have frequent, unpredictable pushes of content, I'm curious to how often data can be pushed to the widget and how quickly the widget will reflect this updated data. Specifically, Is there a maximum number of reloads per day? Is there a throttle to how often we can reload? Is there an upper limit to the time taken between the call to reloadTimeline() and when the reload actually happens? 3a. If events are unpredictable, should we use a timeline at all? (as compared to just a single entry in the timeline at all times) Or should there just be a very finely grained timeline (eg. every minute) that simply does pull requests in the background to see if anything changes? 3b. Or should everything be based on push notifications which reloads the timeline? What if the user has disabled push notifications? For example, a news widget might want to keep the user updated of breaking news which might be relevant only for the next 10 minutes. (eg. "Soccer: Chelsea-Liverpool 3-1 upset at half time!") I am curious how the Apple stock widget is implemented as well. Does it have a 1 minute interval timeline, and each entry attempts to update the content via a background network request? As WidgetKit is really new to us, I would appreciate any guidance from Apple regarding the best technical designs and practices in this area. Thanks so much!
Asked
by hokkaido.
Last updated .
Post marked as unsolved
32 Views

WidgetCenter.reloadTimelines allowed frequency?

I'm writing a widget for a music app. The widget is similar to the Apple Music app's Recently Played widget. I want to call WidgetCenter.reloadTimelines when the currently played song changes, so that the widget refreshes to show the currently played song. Songs last about 3-4 minutes on average, but a user can skip songs or otherwise manually choose different songs more frequently than that. Will my widget be penalized if the reloadTimelines call is made every 3-4 minutes? If so, what is the suggested algorithm for keeping the widget up-to-date in this situation? It's not a good user experience to simply implement 15 minute throttling, as users will mistrust the widget if it is typically several songs out of date. Is it recommended that the app try only refresh the widget when the app loses focus? What about the case of a music app that is playing a playlist of short songs while in the background? In that case the app is not "in focus" in the first place. For reference, here's an example Apple Developer Forum post where the comment is made that changing a timeline once a minute is too fast, and that a target of once every 15 minutes is a reasonable target. https://developer.apple.com/forums/thread/653265
Asked
by jackpal.
Last updated .
Post marked as solved
43 Views

WidgetKit won't request a new timeline at the end

When I run the code snippet below, WidgetKit is supposed to request a new timeline at the end of each minute according to the Apple's documentation. let currentDate = Date() let futureDate = Calendar.current.date(byAdding: .minute, value: 1, to: currentDate)! let timeline = Timeline(entries: [       Entry(date: currentDate, number: Int.random(in: 0...10))], policy: .after(futureDate)) completion(timeline) But it just works once and nothing happens afterward. I am trying it on iOS 14 Beta 2.
Asked
by cmltktl.
Last updated .
Post marked as unsolved
27 Views

Crash on using List in iOS 14 Widget

Xcode 12 crashes on using List in an iOS 14 widget. Fatal error: PlatformViewRepresentableAdaptor&lt;ListRepresentable<SystemListDataSource<Never&gt;, SelectionManagerBox&lt;Never&gt;>>: file /Library/Caches/com.apple.xbs/Sources/Chrono/Chrono-70.4/WidgetKit/SPI/WidgetViewArchive.swift, line 254 2020-07-13 15:34:55.965035+0530 ListWidgetExtension[866:188282] Fatal error: PlatformViewRepresentableAdaptor&lt;ListRepresentable<SystemListDataSource<Never&gt;, SelectionManagerBox&lt;Never&gt;>>: file /Library/Caches/com.apple.xbs/Sources/Chrono/Chrono-70.4/WidgetKit/SPI/WidgetViewArchive.swift, line 254 Will this be fixed in the next betas? or are lists not allowed to be used in a widget? If lists are not allowed then how can I show multiple columns of data?
Asked
Last updated .
Post marked as unsolved
61 Views

Xcode preview for Widget?

Hi. I've been trying to build a widget from the documentation and the WWDC 2020 code-along videos. I've been trying to preview the widget in Xcode using the code from the WWDC Videos, but keep getting an "Unrecoverable error." Is the preview working for Wigets, or is this planned for a future update? BTW, I did try to get a copy of the error, but that doesn't seem to be working either. Thanks, Dan Uff
Asked
Last updated .
Post marked as unsolved
11 Views

Variable WidgetBundle Configurations possible?

I am trying to implement a variable configuration for my WidgetBundle. For example, I would like to have a widget only available for certain customers. The general idea would be some like this like: @main struct Widgets: WidgetBundle {     @WidgetBundleBuilder     var body: some Widget { if Membership.active? { WidgetA() WidgetB() WidgetPremium()     } else { WidgetA() WidgetB() } } } I realize that this particular syntax is invalid, but I've tried all manner of Function builder syntaxes I could think of to make something like this work but haven't been able to get any to work for widgets. In SwiftUI we have the conditional function builders, but I can't quite discover if they don't exist here or I'm just missing them. Is this type of configurability possible? Thank you!
Asked
Last updated .
Post marked as solved
34 Views

iOS widget previews with different size family

While working through Part II the Widget code-along from WWDC, I thought I would add a separate preview for each size, just as an exercise for myself.     static var previews: some View {         Group {             EmojiRangerWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), character: .panda))                 .previewContext(WidgetPreviewContext(family: .systemSmall))             EmojiRangerWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), character: .panda))                .previewContext(WidgetPreviewContext(family: .systemMedium))         }     } } This works, giving me two previews of the correct size. Unfortunately, the smaller preview is trying to layout the medium's view within its given space. I'm assuming that this is because the @Environment for the widgetFamily wasn't configured correctly, so I thought I'd add it like this: .environment(\.widgetFamily, .systemSmall) I was guessing that something like this might be possible, because I have seen a similar situation where a preview is created for a dark mode like this: .environment(\.colorScheme, .dark) Alas, my attempt to set the widgetFamily environment doesn't work. It gives me a this error: Key path value type 'WritableKeyPath&lt;EnvironmentValues, WidgetFamily&gt;' cannot be converted to contextual type 'KeyPath&lt;EnvironmentValues, WidgetFamily&gt;' Am I close? Is there some way to inject the widget family environment, or is there a bug that fails to bind the WidgetPreviewContext(family: .systemSmall) that I'm already passing?
Asked
by pohl.
Last updated .
Post marked as unsolved
6 Views

WidgetKit Display Name/Bundle Display Name

Is there a way to change the name displayed under the widget on the home screen? You can set the Display Name for the widget target and the app target separately, but it always seems to use the app display name on the under the widget on the home screen. Currently I use a shortened name under my app icon, as it's too long to fit! But there's room under a widget for the full app name, so it makes sense to use the full name if possible.
Asked
by iamjamie.
Last updated .