@Environment broken in iOS widgets?

I'm working on an iOS widget and it seems @Environment does not work here at all. I tried using @Environment(\.widgetFamily) but always got .systemMedium. So far not one environment value seems to work in my widget: neither the ones provided by the system, nor custom environment values.

Example:

struct WidgetEntryView : View {
	@Environment(\.widgetFamily) private
	var widgetFamily
	
	@Environment(\.controlSize) private
	var controlSize
	
	var body: some View {
		Text("\(String(describing: self.widgetFamily))/\(String(describing: self.controlSize))")
	}	
}


@main
struct MainWidget: Widget {
	let kind: String = "Widget"
	
	var body: some WidgetConfiguration {
		return IntentConfiguration(kind: kind,
				intent: MyWidgetIntent.self,
				provider: MyTimelineProvider())
		{
			(entry) in
			
			WidgetEntryView()
			.controlSize(.large)
		}
		.configurationDisplayName("Widget Title")
		.description("Widget Description")
	}
}

Using Xcode 14.0 on an iOS 15.5 device, this always prints "systemMedium/regular", no matter which widget size I use or which control size I manually set.

(I can also reproduce this with other views inside my view hierarchy, it's not just affecting the top-level view.) Is this a known problem? Is there a workaround, apart from passing the values I care about as explicit parameters/properties?

Replies

This is my code, and it works fine:

@main
struct WidgetEntry: Widget {
	public var body: some WidgetConfiguration {
		IntentConfiguration(kind: kWidgetKind,
							intent: DynamicEventSelectionIntent.self,
							provider: Provider()
		) { entry in
			WidgetEntryView(entry: entry)
		}
		.configurationDisplayName(NSLocalizedString("AddingWidget_Title", comment: "Adding the Widget"))
		.description(NSLocalizedString("AddingWidget_Description", comment: "Adding the Widget"))
		.supportedFamilies([.accessoryCircular, .accessoryInline, .accessoryRectangular, .systemSmall, .systemMedium])
	}
}

struct WidgetEntryView: View {
	var entry: Provider.Entry
	@Environment(\.widgetFamily) var family

	@ViewBuilder
	var body: some View {
		switch family {
			case .accessoryCircular, .accessoryInline, .accessoryRectangular:
				LockScreenWidgetView(event: entry.event)

			case .systemSmall:
				SmallWidgetView(event: entry.event)

			case .systemMedium, .systemLarge, .systemExtraLarge:
				MediumWidgetView(event: entry.event)

			@unknown default:
				MediumWidgetView(event: entry.event)
			}
		}
	}
}
  • I see you have .supportedFamilies(…) which was missing in my widget. I just added it, but it doesn't make any difference: I still only get .systemMedium, even for the small and large widgets, and none of the other @Environment properties work.

Add a Comment