Adding a Widget extension to Multiplatform app targeting macOS iOS and iPadOS

I am developing an app having multiple targets ("Supported destination") in my Xcode project. I am targeting macOS, iOS and iPadOS with the app.

While the macOS target was active, I added a WidgetKit extension selecting the macOS as target for the widget extension, since there is no option to add a multiplatform WidgetKit target to the project.

Obviously now when switching to iPad or iPhone targets, the app does not build, since the error:

Your target is built for iOS but contains embedded content built for the macOS platform (XxxxWidgetExtension.appex), which is not allowed.

Is there a way to configure the WidgetKit extension to all platforms? I should not have platform dependent code in the extension implementation.

Or must I abandon the app having multiple destinations, and add separate targets for macOS, iOS and iPadOS and add separate WidgetKit extensions to these targets?

Xcode Version 14.3.1 (14E300c), targeting iOS 16.2, macOS 13.1.

Answered by AndyJJ in 754908022

OK, so what I can do is:

  1. Create two widget targets, one for iOS and one for macOS.
  2. In the app target General settings, scroll to Frameworks, Libraries and Embedded Content.
  3. Select, in the Filters section, for each of the extensions, on which platform they should be embedded to, one for each.

That should do it.

Accepted Answer

OK, so what I can do is:

  1. Create two widget targets, one for iOS and one for macOS.
  2. In the app target General settings, scroll to Frameworks, Libraries and Embedded Content.
  3. Select, in the Filters section, for each of the extensions, on which platform they should be embedded to, one for each.

That should do it.

Adding a Widget extension to Multiplatform app targeting macOS iOS and iPadOS
 
 
Q