0

I am building a module for React Native, as a part of this, there is some native iOS code that is wrapped in a cocoapod. I have this working fine, and I can install the cocoapod and see the native classes from the RN side no problem. One of the native classes is trying to get the path to a local mp3 file inside a sub folder proj/Audio/file.mp3. Using the following ObjC code: NSString *path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"mp3" inDirectory:@"Audio"];

This is returning nil and as such causing an error when trying to get the NSURL for the file. I have the mp3 extension added to the podspec @ s.source_files = "ios/**/*.{h,m,swift,mp3}" so it is moving the file over. However, I assume I have to be missing something as it ultimately cannot find the file.

I have gone into the source xcode project and added the mp3 file to the Copy files phase in build phases. I have also made sure that the file was linked to the target. I am not sure what else I could be missing at this point for it to not be able to be found.

David Karasek
  • 348
  • 3
  • 16

2 Answers2

0

Instead of putting the mp3 file in source_files of your podspec, try using resource_bundles; see the documentation here. Basically it lets you define resource bundle files that CocoaPods creates for you with the assets that you specify, for example:

s.resource_bundles = {
'Audio' => ['ios/**/*.mp3']
} 

You can load the bundle file resource that CocoaPods created (create a NSBundle object from its URL by its name - Audio in the above example, and extension - bundle), then get a path from resources inside this bundle (the same method that you used on the mainBundle).

Artal
  • 8,933
  • 2
  • 27
  • 30
0

I know this is an old post, but I was struggling with this myself. I completely agree with Artal, to sum up you need to

let bundlePath = Bundle.main.path(forResource: "Audio", ofType: "bundle");
let bundle = Bundle(path: bundlePath!)

maybe guard your the path,