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:
Code Block Swift
@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!
Post not yet marked as solved Up vote post of UnderscoreDS Down vote post of UnderscoreDS
2.1k views

Replies

This isn't possible—the configuration returned in a WidgetBundle or Widget is best considered static. If you have a use case that we don't currently support please do let us know via Feedback Assistant!
I definitely see a use case for it and submitted a feedback! In my case, I have a HealthKit-based widget that isn't functional on iPads, so I'd like to only expose it to iPhone users.

Feedback ID: FB8454119
  • Hey @Khuffie! Were you able to find a solution for filtering out the widget from iPads? I'm in a similar boat where I want to disable a widget for iPads but not sure how to do that.

Add a Comment

I found a workaround

https://www.avanderlee.com/swiftui/variable-widgetbundle-configuration/

@main
struct StockAnalyzerWidgets: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        widgets()
    }

    func widgets() -> some Widget {
        if #available(iOS 16.0, *) {
            return WidgetBundleBuilder.buildBlock(StockAnalyzerWatchlistWidgets(), StockAnalyzerSymbolWidgets())
        } else {
            return StockAnalyzerWatchlistWidgets()
        }
    }
}

Any body using this in production environment?

  • Do you know how to perform it but with a regular if instead the if #available?

Add a Comment

@UnderscoreDS did you solve it?