5

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()
    }
}
Mark
  • 51
  • 3
  • Does this answer your question? [Check OS version in Swift?](https://stackoverflow.com/questions/24503001/check-os-version-in-swift) – pkamb Jun 30 '22 at 17:58
  • @pkamb I wish but I tried that. See my comment on the answer below. That's the error message I get when I try that. – Mark Jul 01 '22 at 14:03
  • 1
    answer supplied here https://stackoverflow.com/questions/72688852/how-can-we-add-a-lock-screen-widget-requiring-ios-16-and-still-support-ios-15 – lewis Jul 16 '22 at 15:53
  • How about adding new target for just lock screen widget? You can specify deployment target over iOS 16. I did not try yet. – sekitaka Aug 08 '22 at 06:52

2 Answers2

8

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)
    }
}
Kaixin Lian
  • 204
  • 2
  • 5
0
var body: some Widget {
    widgets()
}

func widgets() -> some Widget {
    if #available(iOS 16.0, *) {
        return WidgetBundleBuilder.buildBlock(LockscreenWidget1(), Widget1())
    } else {
        return Widget1()
    }
}