简体   繁体   中英

Lockscreen swift widget only available to iOS 16 users

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

Just use the #available attribute instead:

import WidgetKit
import SwiftUI

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

This will make the entire struct work on iOS 14 while making the smaller lock screen subset just for iOS 16+. You might be interested in this forum discussion .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM