iosswiftreact-nativereact-native-native-modulenative-module

How to import an asset in swift using Bundle.main.path() in a react-native native module


I'm making an alarm native module (and library) and I need to import an mp3 for the sound of the alarm notification. I'm trying to import the mp3 using swift with the following code: guard let filePath = Bundle.main.path(forResource: "bell", ofType: "mp3") else {fatalError()}

However, it's always falling in fatalError because it can't find the asset. This code worked in a pure swift IOS application. I tried seeing if the asset was in the bundle with the code:

if let files = try? FileManager.default.contentsOfDirectory(atPath: Bundle.main.bundlePath) {
            print(files)
}

But it does not appear in it: Files in the bundle

It also does not appear in the Pods part in XCode: Pods part in XCode

It is, however, in the folder IOS in the native module: bell.mp3 in the folder of the native module

Any help is appreciated. Thanks.

UPDATE

I put the file in the Copy Files of the project and it worked! However, now I have another problem. This is a native module/library that will be installed via npm, do I need to instruct every user to add the bell.mp3 to the Copy Files or is there a way to automatically configure this?


Solution

  • After trying a lot of solutions I found a solution that works for me, without the need of adding the file to the Copy Files section of build phases.

    What I did, was adding the bell.mp3 in the .podspec file as a resource, like this:

    s.resource = "ios/bell.mp3"

    Full .podspec file:

    require "json"
    
    package = JSON.parse(File.read(File.join(__dir__, "package.json")))
    folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
    
    Pod::Spec.new do |s|
      s.name         = "expo-alarm-module"
      s.version      = package["version"]
      s.summary      = package["description"]
      s.homepage     = package["homepage"]
      s.license      = package["license"]
      s.authors      = package["author"]
    
      s.platforms    = { :ios => "11.0" }
      s.source       = { :git => "[git_url]", :tag => "#{s.version}" }
    
      s.source_files = "ios/**/*.{h,m,mm,swift}"
      s.resource = "ios/bell.mp3"
    
      # Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
      # See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
      if respond_to?(:install_modules_dependencies, true)
        install_modules_dependencies(s)
      else
      s.dependency "React-Core"
    
      # Don't install the dependencies when we run `pod install` in the old architecture.
      if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
        s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
        s.pod_target_xcconfig    = {
            "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
            "OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
            "CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
        }
        s.dependency "React-Codegen"
        s.dependency "RCT-Folly"
        s.dependency "RCTRequired"
        s.dependency "RCTTypeSafety"
        s.dependency "ReactCommon/turbomodule/core"
       end
      end    
    end
    

    After doing this and running pod install in the ios folder of the react native app, it worked.