Please note, this question is regarding Xcode 15 beta 4, iOS 17 beta 3, and watchOS 10 beta 3
I have a watchOS 10 App with iOS 17 companion app with two Widget (WidgetKit) Extension Targets. Both targets share the same codebase where each Swift file has both targets set in the Target Membership settings.
Some parts of my code are behind #if os(watchOS)
compiler directives, for example when setting the supported widget families:
#if os(watchOS)
.supportedFamilies([.accessoryCircular, .accessoryRectangular])
#else
.supportedFamilies([.accessoryCircular, .systemSmall, .systemMedium])
#endif
I got multiple Widgets that are made available through a WidgetBundle
. However, the following issues also exist when there is only one widget.
This is my current WidgetBundle
and preview code:
@main
struct WidgetExtensionBundle: WidgetBundle {
var body: some Widget {
StaticSampleWidget()
if #available(iOSApplicationExtension 17.0, *) {
AppIntentSampleWidget()
}
}
}
#if os(watchOS)
#Preview(as: .accessoryRectangular) {
StaticSampleWidget()
} timeline: {
StaticSampleWidget_Entry(date: .now, emoji: "π")
StaticSampleWidget_Entry(date: .now, emoji: "π€©")
}
#else
#Preview(as: .systemSmall) {
StaticSampleWidget()
} timeline: {
StaticSampleWidget_Entry(date: .now, emoji: "π")
StaticSampleWidget_Entry(date: .now, emoji: "π€©")
}
#endif
When I try to use the preview in Xcode, it cannot build the app and show no previews. By trying different settings and combinations of selected targets, I sometimes get at least the iOS preview running, but never the watchOS preview. Also, the first #Preview
behind the #if os(watchOS)
is grayed out. On the Canvas it always shows two Preview tabs, even though it should only be one depending on the selected target. I sometimes get the following errors, and sometimes it doesn't tell me at all what is wrong:
Sample.watchkitapp.WidgetExtensionWatch.appex with id xxx.xxx.Watch- Sample.watchkitapp.WidgetExtensionWatch does not define either an NSExtensionMainStoryboard or NSExtensionPrincipalClass key with a string value
Failed to build WidgetExtensionBundle.swift
How do I correctly setup my targets, and preview code to be able to show the relevant previews for iOS and watchOS?