How can I add more than 5 widgets in a widget extension? the maximum number of WidgetBundle's widget cannot exceed 5

Viewed 978

It seems that WidgetBundle has a maximum number limit, if it exceeds 5, a compile error will be reported: Extra argument in call.

But I have not seen such a description in any document, and no other developers mentioned this issue.

Does anyone have an idea?

@main
struct WidgetsBundle: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        Widget1()
        Widget2()
        Widget3()
        Widget4()
        Widget5()
        Widget6() // Extra argument in call
    }
}
2 Answers

I am experiencing same error within WidgetBundle. If you are interested in having more than 5 widgets for the app, there is a workaround:

@main
struct WidgetKitExtension: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        Widget1()
        Widget2()
        Widget3()
        Widget4()
        Bundle2().body
    }
}

struct Bundle2: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        Widget5()
        Widget6()
        Widget7()
    }
}

Basically, you create second WidgetBundle and use it in your first one. I have verified that this works as of Xcode 12.0.1

Haven't tested submitting the app with more than 5 widgets though.

For information, the limit of five widgets is because the WidgetBundleBuilder result builder only had versions of buildBlock for 1-5 entries.

You can see this limit by selecting the WidgetBundleBuilder in your code, right clicking and selecting "Jump to Definition". Scroll down a little and you will see overloads in SwiftUI for buildBlock going as high as five template parameters.

I just noticed that Xcode 14 / iOS 16 has raised this limit to ten entries: there are now buildBlock overloads going as high as ten template parameters.

This is a compile-time limitation. An Xcode 14 build using up to ten widgets in a single WidgetBundleBuilder's body method now compiles and works when run on iOS 15 (I've not tried iOS 14). The workaround would still be required to exceed 10 widgets.

Related