Platform-Specific Resources in Catalyst Apps.
How do I specify platform-specific alternatives for my resources?
In an
Asset Catalog
, you can simply fiddle with the checkboxes in theDevices
section of theAttributes Inspector
.
But what about resources that cannot be in an Asset Catalog?
In a traditional multiplatform app, with a target for each supported platform, you can use the
Target Membership
section of theFile Inspector
to choose which resources are built into which target.
But what if I’m working on a single-target Catalyst app?
In your target’s setting, the
Build Phases
section will have aCopy Bundle Resources
phase. Its table has aFilters
column you can use to specify that a resource is to be copied only on a specific platform.
But what if the resource, for reasons outside of my control, needs to end up in the exact same place on both platforms?
Well I couldn’t figure it out, but DTS Engineer Ed Ford came up with a clever trick, and I felt like sharing.
The Trick
Let’s assume you want your target to contain a resource named exactly stuff.data
in a directory named exactly resources
, but you have two separate versions of this file – one for iOS and one for macOS Catalyst.
Before adding anything to your Xcode project, create this directory structure:
all_resources/
├─ iOS/
│ ├─ resources/
│ ├─ stuff.data <- iOS version
├─ macOS
├─ resources/
├─ stuff.data <- macOS version
Now in Xcode, using File
, Add Files to "ProjectName"
, add each of the resources
directories to your project. Make sure to uncheck all targets from the Add to targets:
section. The directories will show up in the project tree as identically named siblings.
Next, in the target’s settings, navigate to Build Phases
and look under Copy Bundle Resources
. Using the +
button, add both resources
directories to the phase. It will not be possible to distinguish them before adding them, but don’t worry: you can’t add the same directory twice, and you will be able to tell them apart once added.
Finally, for each directory in this list, change the value in the Filters
column using the little filter button. Leave only the appropriate platform checked.
Check out my devlog on Mastodon for more app development tips.