Mastodon
Skip to content

Question (Answered!): Can you create a Widget Bundle that targets specific platforms?

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

Updated 2022-08-31: Thanks to Jefferson for this hint. You don't create a WidgetBundle that targets multiple platforms; you create separate widget extensions targeting each platform.

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