What is the proper way of adding ActivityKit Widget to existing WidgetBundle supporting iOS 15?

I have an existing WidgetBundle containing a single widget today that works with iOS 14/15. When I add the ActivityKit widget (aka Live Activities), Xcode naturally complains that this is available iOS 16 only.

Adding an #if available(iOS 16.0, *) check inside the WidgetBundle results in an error: "Closure containing control flow statement cannot be used with result builder 'WidgetBundleBuilder'"

Would the proper mechanism here be to create a second widget extension that ONLY includes the widget for the Live Activity and then use the available modifier on the entire widget bundle to only include it in iOS 16+?

This thinking is based off the following here

Typically, you include all your widgets in a single widget extension, although your app can contain multiple extensions. For example, if some of your widgets use location information and others don’t, keep the widgets that use location information in a separate extension. This allows the system to prompt the user for authorization to use location information only for the widgets from the extension that uses location information.

Hi!

WidgetBundleBuilder doesn't support control statements at the moment, however, there's a workaround currently available. Can you give this a try?

var body: some Widget {

        if #available(iOS 16.0, *) {

            return WidgetBundleBuilder.buildBlock(ActivitySessionWidget(), ExistingHomeScreenWidgets())

        } else {

            return ExistingHomeScreenWidgets()

        }

    }

Ok, i’m lost with this one. I have a widget bundle and i need to have different widgets for different versions of iOS. Can someone explain to me why the following code works fine, but if I uncomment those 2 lines it starts giving me a Function declares an opaque return type 'some Widget', but the return statements in its body do not have matching underlying types error?

@main
struct MyWidgetsBundle: WidgetBundle {
    var body: some Widget {
        if #available(iOS 17.0, *) {
            return WidgetBundleBuilder.buildBlock(
                A(),
                B(),
                
                C(),
                
                D()
            )
        } else if #available(iOS 16.1, *) {
            return WidgetBundleBuilder.buildBlock(
//                A(),
//                B(),
                
                C(),
                
                D1(),
                D2()
            )
        } else {
            return WidgetBundleBuilder.buildBlock(
                C(),
                
                D1(),
                D2()
            )
        }
    }
}

@PinStudios Where you able to solve this? Im having the same issue, was able to solve it for lower version with the code below, but then it stops working for iOS 17.

struct MyWidgetBundle: WidgetBundle {
    
    var body: some Widget {
        if #available(iOS 17, *) {
            // If iOS 17 allow both widgets + live activity
            return WidgetBundleBuilder.buildBlock(MyWidget(), MyFavouriteWidget(), MyWidgetLiveActivity())
            
        } else {
            // This widgets wrap other validations to allow different widgets depending of the version
            return Widgets()
        }
    }
    
    private func Widgets() -> some Widget {
        if #available(iOS 16.1, *) {
            // If iOS 16.1 allow Favourite widget + live activity
            return WidgetBundleBuilder.buildBlock(MyFavouriteWidget(), MyWidgetLiveActivity())
        } else {
            // If lower just allow Favourite Widget
            return MyFavouriteWidget()
        }
    }
}
What is the proper way of adding ActivityKit Widget to existing WidgetBundle supporting iOS 15?
 
 
Q