Preview and bundle for modular app target

I have a dummy project that is set up in the following way:

  • one Xcode project
  • two frameworks, UI and Feature, Feature embeds UI-framework
  • three app targets, two that are SwiftUI lifecycle and one UIKit lifecycle
  • all app targets import the Feature framework
  • all app targets have same setup for asset catalogue

The point of this setup is to be able to switch between app target scheme and see preview changes accordingly. That works 100% with anything coming from the asset catalogue and even localization catalogues.

The dummy project exists as a proof of concept for the main project (8 years old, all Swift) I am working on. However, the same setup does not work in the main project.

I am suspecting it has to do with how the project is initialised and bundles. I print out the bundles to see which bundles are registered.

Dummy project will print out the bundle based on app target, the main bundle being based on the selected scheme.

The main project will have the previews.com.apple.PreviewAgent.iOS as main bundle.

Hi,

Sorry to hear you are having problems getting previews working in your older project. There were some issues in older Xcode releases where Bundle loading would fail when using the new previews execution mode. Some things you could try to see if you are hitting these are:

  • try switching to the legacy mode using Editor > Canvas > Use Legacy Previews Execution
  • Update to the latest shipping Xcode

If the latter does not help, then the best next step will be to file a feedback with diagnostics so we can take a look.

We will need the diagnostics Xcode Previews generates in order to make sure we understand the error the previews system is encountering.

Install the logging profile using instructions available here: https://developer.apple.com/bug-reporting/profiles-and-logs/?name=swift On your mac running Xcode, and on your physical preview device (if you are using one).

Install the logging profile using the following instructions on your mac running Xcode; and if you are using one, your physical preview device (iOS or visionOS): https://developer.apple.com/bug-reporting/profiles-and-logs/?name=swift

Then when you reproduce the problem in Xcode:

  1. Either (a) an error banner will appear, click the "Diagnostics" button in that banner; or (b) if you're not seeing an error but you still want to provide diagnostics you can get the same diagnostics window by going under the Editor menu in the menu bar, then selecting the Canvas submenu, then selecting "Diagnostics".
  2. In the sheet that appears, click "Generate Report" in the bottom left of the sheet
  3. Attach (or make from the folder) the resulting zip file to the bug (will be named something like previews-diagnostics-0123456789.zip)
  4. Generate a sysdiagnose on your mac and any on-device preview devices, and attach those too

Here is a better explanation of the problem:

I am working on implementing SwiftUI. I want to take advantage of SwiftUI Preview on a project, but I am currently experiencing issues with getting access to app target resources.

Essentially I want to use app target schemes, and their bundled resources, to preview SwiftUI.

I have created a dummy project in order to test out my basic assumptions and verify my premise. The dummy project consists of two frameworks and three targets in the same project.

Two targets have the SwiftUI setup, the last one has a UIKit setup. One framework is the UI-framework, and there is another framework called Feature.

  • App targets: SwiftUI-One, SwiftUI-Two, UIKit-One
  • Frameworks: UI, Feature

The Feature framework contains the UI framework. This is to resemble the current setup in my project. App targets contains the Feature framework.

Each app target has an asset catalogue and a localization catalogue. The structure (naming) of these are the same, only the assets themselves are different.

With this setup I can create a View in the UI-module and reference images, colors and localizations, and use app target schemes to see how it looks for each target. This works 100% of the time, flawlessly.

When I recreate the same structure in my main project it does not work. However, there are some important differences in the test project and main project.

The test project has targets within the same Xcode project. In the main project there is a workspace containing different projects and targets.

Test project (Xcode project)

  • SwiftUI-One (app target)
  • SwiftUI-Two (app target)
  • UIKit-One (app target)
  • UI module (framework)
  • Feature module (framework)

Main project (Xcode Workspace)

  • App 1 project with app target
  • App 2 project with app target
  • UI project with framework target
  • Feature project with framework target

The result of the structure of main project is that assets are not available to Preview by default, this is the practical difference from test project which is the goal.

I have also recreated the test project setup with the same structure (workspace + Xcode projects) to verify that is the problem.

When targets live in the same project I have access to asset resources based on target and scheme, but not when targets exist across projects.

My assumptions is how bundling works. Test project will show for example 'com.SwiftUI-One.dev' as the main bundle, and the main project will have a 'previews.com.apple.PreviewAgent.iOS' as main bundle.

Preview will still essentially work, but it will not retrieve any assets at all. When printing out all bundles I do not see the bundle for the app target itself, nor am I able to manually retrieve it using the bundle identifier.

I've also tried to use xcconfig-files combined with schemes, first to reference all assets in build phase and then to remove assets I don't want to add, but that just creates more work than it's worth.

There is no easy way to recreate the current main project to fit the functioning test project structure, so I am looking for alternative ways to solve this problem.

Preview and bundle for modular app target
 
 
Q