skip to Main Content

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()
    }
}

2

Answers


  1. 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)
        }
    }
    
    Login or Signup to reply.
  2. var body: some Widget {
        widgets()
    }
    
    func widgets() -> some Widget {
        if #available(iOS 16.0, *) {
            return WidgetBundleBuilder.buildBlock(LockscreenWidget1(), Widget1())
        } else {
            return Widget1()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search