Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to conditionally render widgets in iOS #984

Open
onmyway133 opened this issue Sep 25, 2024 · 0 comments
Open

How to conditionally render widgets in iOS #984

onmyway133 opened this issue Sep 25, 2024 · 0 comments

Comments

@onmyway133
Copy link
Owner

The WidgetBundle lets us expose multiple widgets from a single widget extension

It uses WidgetBundleBuilder to constructs a widget bundle’s body.

In iOS 18, if we include ControlWidget then we need to check iOSApplicationExtension iOS 18. For now in Xcode 16 there's a bug that prevents existing widgets from appearing in iOS 17.

We can leverage WidgetBundleBuilder to conditionally render widgets for each iOS version

import SwiftUI
import WidgetKit
import Widgets

@main
struct OurAppWidgetBundle: WidgetBundle {
    var body: some Widget {
        if #available(iOSApplicationExtension 18.0, *) {
            return iOS18Widgets
        } else {
            return iOS17Widgets
        }
    }

    @available(iOSApplicationExtension 18.0, *)
    @WidgetBundleBuilder
    private var iOS18Widgets: some Widget {
        BookingsWidget()
        TransactionsWidget()
        BookControlWidget()
        RefundControlWidget()
    }

    @WidgetBundleBuilder
    private var iOS17Widgets: some Widget {
        BookingsWidget()
        TransactionsWidget()
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant