Lockscreen swift widget only available to iOS 16 users

Viewed 741

I have multiple widgets that are for iOS 14 users and above. But with the new lockscreen widgets, it's only available to iOS 16 users. How can I only make the bottom two widgets for iOS 16 users? If I uncomment the top line then I believe it will make all widgets only available to iOS 16 users but I can't do that, I want my users to be able to continue using the home screen widgets if they're on iOS 14-15.

import WidgetKit
import SwiftUI


//@available(iOSApplicationExtension 16.0, *)
@main
struct Widgets: WidgetBundle {
    @WidgetBundleBuilder
    var body: some Widget {
        Widget1()
        Widget2()
        Widget3()
        LockscreenWidget1()
        LockscreenWidget2()
    }
}
1 Answers

You need to configure it inside your Widget logics.

e.g.

@main
struct MyWidget: Widget {
    
    private let supportedFamilies:[WidgetFamily] = {
        if #available(iOSApplicationExtension 16.0, *) {
            return [.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge, .accessoryInline, .accessoryCircular, .accessoryRectangular]
        } else if #available(iOSApplicationExtension 15.0, *) {
            return [.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge]
        } else {
            return [.systemSmall, .systemMedium, .systemLarge]
        }
    }()
    
    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            MyWidgetEntryView(entry: entry)
        }
        .configurationDisplayName("Demo Name")
        .description("Description of the demo.")
        .supportedFamilies(supportedFamilies)
    }
}
Related