iosswiftwidgetkitwidgetliveactivity

Can I support supplementalActivityFamilies and also iOS17?


I have a live activity in my app, and beginning with iOS 18 these automatically appear on WatchOS and you can modify its appearance using supplementalActivityFamilies([.small]). My live activity supports iOS 17 and i would like to continue to support iOS 17 but would like to modify the appearance for iOS 18 devices. I figured this would be a simple if #available(iOS 18, *) but i run into an error

Branches have mismatching types 'some WidgetConfiguration' and 'ActivityConfiguration<OutageAttributes>'

    var body: some WidgetConfiguration {
        
        if #available(iOS 18, *) {
            ActivityConfiguration(for: OutageAttributes.self) { context in
                OutageActivityView(outage: context.attributes.outage, outageStatus: context.state, isStale: context.isStale, activityId: context.activityID)
            } dynamicIsland: { context in
                createDynamicIsland(context: context)
            }
            .supplementalActivityFamilies([.small])
        } else {
            ActivityConfiguration(for: OutageAttributes.self) { context in
                OutageActivityView(outage: context.attributes.outage, outageStatus: context.state, isStale: context.isStale, activityId: context.activityID)
            } dynamicIsland: { context in
                createDynamicIsland(context: context)
            }

        }
    }

Is there any way to conditionally support iOS18 features in iOS18 while keeping iOS17 target?


Solution

  • You can define the following extension:

    extension ActivityConfiguration {
        func addSupplementalActivityFamilies() -> some WidgetConfiguration {
            if #available(iOS 18.0, *) {
                return self.supplementalActivityFamilies([.small])
            } else {
                return self
            }
        }
    }
    

    and apply it to your ActivityConfiguration:

    var body: some WidgetConfiguration {
        ActivityConfiguration(for: OutageAttributes.self) { context in
            OutageActivityView(...)
        } dynamicIsland: { context in
            createDynamicIsland(context: context)
        }
        .addSupplementalActivityFamilies()
    }