0

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.

West1
  • 1,430
  • 16
  • 27
  • Could you show the code you are using for step 4. ? Also what are the steps you are taking to add the xcassets file to both targets? – Wez Apr 19 '21 at 08:12

1 Answers1

1

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.

West1
  • 1,430
  • 16
  • 27
  • 1
    Nice one, I was just about to post a reply as looking at [Apples Documentation](https://developer.apple.com/documentation/spritekit/skaction/1417664-playsoundfilenamed), it kinda says this in not so many words - "The name of a sound file in the app’s bundle." – Wez Apr 19 '21 at 09:24