I'm working on a native react native module, where I need a JSON resource file. I cannot figure out how to make this file available for the iOS side for either unit testing in the module or when serving a react native application.
I've been trying to put the file into a folder (Resources or SupportFiles as folder name makes no difference) on the root level of the ios-folder of the module. When I drag it to the XCode (9.3.1) project navigator, it gets added to the "Tests"-target of the module (the lego brick icon), under Build Phases --> Copy Bundle Resources. I have also tried adding it to Build Phases --> Copy Files under the real module build targets (the ones with facade-icons).
Running NSLog(@"%@", [[NSBundle mainBundle] bundlePath]); using a unit test I get a path:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents
Neither my file nor the folder get transferred to that path. If I manually copy the file there it opens without problems from my code (as accessed from the mainBundle, but that is not a sustainable solution.
I was wondering whether the resource files would only be copied to destination during a full build operation? The module builds successfully for the unit tests, but a full build has not been defined, as the module is not intended to run by itself. I also tried building my test app, which exercises the module, but couldn't find the file in any of the build results there either.
All in all this should be trivial (and on the Android-side it was). What am I missing?
I added my resource file to my .podspec file with
s.resource = "ios/models/de/myfile.abc"
Full .podspec:
require "json"
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
Pod::Spec.new do |s|
s.name = package["name"]
s.version = package["version"]
s.summary = "SUMMARY"
s.homepage = "HOMEPAGE"
s.license = package["license"]
s.authors = package["author"]
s.platforms = { :ios => "11.0" }
s.source = { :git => "myGitUrl", :tag => "v#{s.version}" }
s.source_files = "ios/**/*.{h,m,swift}"
s.swift_version = "5.0"
s.dependency "React"
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.resource = "ios/models/de/myfile.abc"
end
You can get the file path in Swift 5 with:
let path: String = Bundle.main.path(forResource: "myfile", ofType: "abc")!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With