Skip to content

Question: Can you create a Widget Bundle that targets specific platforms?

Stuart Breckenridge
Stuart Breckenridge
1 min read
Question: Can you create a Widget Bundle that targets specific platforms?

This limitation of WidgetKit is driving me nuts: it simply doesn't seem possible to create a WidgetBundle that contains widgets that target specific platforms. The @WidgetBundleBuilder doesn't support control flow statements.

In the example below, I am trying to create a WidgetBundle that contains a widget that is only available on iOS 16. Alas, no joy.

@available (iOSApplicationExtension 16, *)
struct HomeScreenCountWidget: Widget {
	let kind: String = "com.ranchero.NetNewsWire.HomeScreenCountWidget"
	
	var body: some WidgetConfiguration {
		return StaticConfiguration(kind: kind, provider: Provider()) { entry in
			SmartFeedSummaryWidgetView(entry: entry)
				.frame(maxWidth: .infinity, maxHeight: .infinity)
				.background(Color("AccentColor"))
		}
		.supportedFamilies([.accessoryRectangular])
	}
}


// MARK: - WidgetBundle
@main
struct NetNewsWireWidgets: WidgetBundle {
	@WidgetBundleBuilder
	var body: some Widget {
		UnreadWidget()
		TodayWidget()
		StarredWidget()
		if #available(iOSApplicationExtension 16, *) {
			HomeScreenCountWidget() // Closure containing control flow statement cannot be used with result builder 'WidgetBundleBuilder'
		}
	}
}

Is there an alternative strategy?

📱App Development🧑🏻‍💻 Code