Search code examples
swiftxcodexcasset

Xcode: Share assets between multiple targets


I have an Xcode project with two targets. I want to share assets between the two targets so as to avoid duplicate files.

What I've tried:

  1. Create SharedAssets.xcassets
  2. Add SharedAssets.xcassets to both targets (when you create the .xcassets folder, you can select which targets to add it to)
  3. Add a .wav file to SharedAssets.xcassets
  4. Reference the .wav file in code

Result:

The .wav file can't be found by either target.

Question:

Why are both targets unable to find the .wav file? How do I share assets between two targets?

Thank you!

Edit:

I'm using a SpriteKit SKAction to load the file from inside an SKScene:

private var sound: SKAction?

override func didMove(to view: SKView) {
    sound = SKAction.playSoundFileNamed("mySound.wav", waitForCompletion: false)
}

I'm then playing the sound like this:

if let action = sound {
   self.run(action)
}

This strategy works perfectly if I reference a .wav file in the main bundle, but fails when referencing the file in SharedAssets.xcassets.

Edit #2:

I am able to successfully use images stored in SharedAssets.xcassets -- so, it's a problem with sounds.


Solution

  • Instead of putting the .wav file in SharedAssets.xcassets, I kept it where it was in the bundle and simply added it to the second target under "Target Membership".

    Doing this for each .wav file solved the problem.