Home > other >  How can we add a Lock Screen Widget (requiring iOS 16) and still support iOS 15?
How can we add a Lock Screen Widget (requiring iOS 16) and still support iOS 15?

Time:06-22

How can we add a Lock Screen Widget to an existing Widget Bundle and still support iOS 15? :thinking_face: for ex this won't compile

struct SecondExtraBundle: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        DailyHeartRatesWidget()
        if #available(iOSApplicationExtension 16.0, *) {
            LockScreenRecoveryScoreWidget()//Requires iOS 16? 
        } else {
            // Fallback on earlier versions
        }
    }
    
}

CodePudding user response:

I think this should work. Simply return an EmptyWidgetConfiguration in case the widget is not supported.

struct SomeWidgetBundle: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        AlwaysAvailableWidget()
        LockScreenWidget()
    }
}

struct LockScreenWidget: Widget {
    var body: some WidgetConfiguration {
        if #available(iOSApplicationExtension 16.0, *) {
            return StaticConfiguration(
                kind: "some.kind",
                provider: LockScreenWidgetTimelineProvider()
            ) { provider in
                Text("Some view")
            }
            .configurationDisplayName("Some display name")
            .description("Some description")
            .supportedFamilies([.accessoryCircular])
        } else {
            return EmptyWidgetConfiguration()
        }
    }
}

This did not work before Xcode 14, but SE-0360 is already implemented and you can do this now.

  • Related