iOS Assets

Hi, is there anyway I can add graphics assets to the iOS bundle and access then via the file system or is the only way to build them into the package by using BinaryBuilder?

thx

i use the Custom Xcode Resource Folders settings for this, then they’re available via NSBundle pathForResource:ofType:inDirectory

1 Like

thx - will try this out

Do you have an example of this, not really sure what should go in the setting. Absolute paths to somewhere? Somewhere in the project tree?

also, what Juce calls access the data?

thanks

if you want to access a folder Foo in your project root you first add it to Custom Xcode Resource Folders - this will cause it to be copied into the app bundle that’s built. then to find the path to Foo/bar.txt you can do one of two things.

  1. if you want to keep things cross-platform then you need to read this thread as it describes how to implement a getSharedResourceFolder() method which will point correctly to assets folders across various os’s. then you can call getSharedResourceFolder().getChildFile("Foo/bar.txt")

  2. if you’re just focused on the mac you can use the native functionality.

const juce::File pathToFoo() {
  NSBundle *mainBundle = [NSBundle mainBundle];
  NSString *filePath = [mainBundle pathForResource:@"bar"
                                            ofType:@"txt"
                                       inDirectory:@"Foo"];
  juce::File file([filePath UTF8String]);
  return file;
}

and this will work on ios and macos (which have slightly different app bundle structures).
note this will have to be in a .mm file for it to compile correctly, and that may cause issues with cross-platform builds (see the above thread for more info there)

1 Like

thanks so much for this. will try it later.

ok, have the resources copied over. I don’t appear to have any .mm files in my project.

If I look at the core Juce libraries, they all have a .mm in the JUCE Library code folder which I can see you can then include other .mm files from.

image

For my modules, I have a .cpp file and not a .mm file, from which I can’t include an .mm file - well I can, but it doesn’t know what NSBundle etc is.

I’ve looked at some juce library description files and there doesn’t seem to be anything obvious that indicates to create a .mm instead of a .cpp.

Any ideas? thx

You can do like almost all JUCE modules: add a .mm file that includes the .cpp (see juce_core.mm for instance). Then you can add your Objective-C++ code directly to that .mm file, or to another .mm file that you include in the main .mm file.

I hope this helps!

hi, thx for the reply.

So I’ve added a .mm file to my module, and made that include the .cpp file and the example code given above, just to see if it will compile, but I get a number of errors seeming that it doesn’t recognise the NSBundle class:

NSBundle is defined in the Foundation macOS framework. You need to add

#import <Foundation/Foundation.h>

at the top of your .mm file.

ok, great thx.

So I have my function

const juce::File path_to_resource( const juce::String& path ) {

in my .mm file, and it builds. how do I call into this from C++?

thx

so i see what’s going on here, looks like an ordering issue.

the .mm is included, which includes the .cpp which includes all the .cpp files for the module, one of which tries to use the function, but the function isn’t defined in the .mm file until after the inclusion of the .cpp file…

I’ve scooted things round a bit and all good.

Thanks for everyone’s help with this. I now understand a bit more of how the ObjC stuff fits together.

Hi. so this is fine for iOS and standalone, but not plugins.

Is there a solution to this where I can pick the resources up out of the main application bundle? I guess this would involve picking up the path to the files in Applications - is there an official way of doing this?

thanks