Any way to save URL into ValueTree on iOS?

I’m using URL to open local file directly following official documents, everything works pretty well, but it can’t get called when I save and re-open the project. It seems like I saved the URL wrong by converting it to a string and save it into a property of the valuetree.

My question is is there any way to directly save the URL into valueTreeState so next time it can get recalled automatically? Thanks in advance!!!

Can you share the code how you set and retrieve the URL to and from the property?
This should be pretty painless and is the recommended way.

1 Like

Ok, sorry for my ignorance. Here’s how I did it:

mFilePathURL is a URL, I put it into my valueTreeState,

mAPVTS.state.setProperty("currentFilePath", mFilePathURL.toString(true), nullptr);

then I get it in getStateInformation and recover it:

void HelloSamplerAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
    juce::MemoryOutputStream mos(destData, true);
    mAPVTS.state.writeToStream(mos);
}

void HelloSamplerAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
    auto tree = juce::ValueTree::readFromData(data, sizeInBytes);
    
    if( tree.isValid() )
    {
        mAPVTS.replaceState(tree);
        
        auto filePath = mAPVTS.state.getProperty("currentFilePath", "").toString();
        if(filePath.isNotEmpty())
        {
            auto filePathURL = juce::URL(filePath);
            HelloSamplerAudioProcessor::loadFile (filePathURL);
        }
    }
}

Everything works fine on Mac and PC, only on iOS, it cannot recall properly when re-open the saved project.

That looks good to me.

I think you can use mFilePathURL.toString(false), since for a local file get-parameters are pretty useless.

Can you check in the debugger what the string is when you read in setStateInformation?

This is the string when I setProperty:

file:///private/var/mobile/Containers/Data/Application/214BBBE9-4839-4A2D-9E17-F304BA3F692A/Documents/songs/20201205-lofi%20test/sampler/1.wav

(I don’t know how to debug and get the setStateInformation string after kill and re-open the DAW :joy:)

It could be related to sandboxing… not sure if the GUID is the same after a rebuild and such…

I’ll have to pass on to someone with more iOS experience, since it’s likely platform related.
The URL is there, but I don’t know if the location is there where it points to.

Seems you got the answer in your other thread:

Yeah, but not sure if it works, I’ll try it later, thanks for the help!