Hi All,
I’m trying to port some code to iOS and I am running into an issue with downloadToFile(). The download starts without issue and appears to get to completion but this code always outputs “Download failed with code: -1” (the file never actually gets written to but the data appears to be downloaded). This code works flawlessly on MacOS. Anyone know what I’m doing wrong? Is there some extra permission required for download task to actually save?
static void download(juce::String url, juce::String fileName, bool &isDownloaded, bool &downloadError)
{
#if JUCE_IOS
auto downloadFolderLocation = juce::File::getContainerForSecurityApplicationGroupIdentifier(APP_GROUP_ID).getChildFile("Downloads");
#else
auto downloadFolderLocation = juce::File(juce::File::getSpecialLocation(juce::File::userHomeDirectory).getChildFile("Downloads"));
#endif
juce::URL download(url);
juce::File fileToWrite(downloadFolderLocation.getChildFile(fileName));
juce::URL::DownloadTaskOptions downloadOptions;
#if JUCE_IOS
downloadOptions = downloadOptions.withSharedContainer(APP_GROUP_ID);
#endif
std::unique_ptr<juce::URL::DownloadTask> taskProgress = download.downloadToFile(fileToWrite, downloadOptions);
if (taskProgress)
{
while (taskProgress->isFinished() == false)
{
juce::Thread::sleep(100);
std::cout << "Downloading Files:" << taskProgress->getLengthDownloaded() / (float) taskProgress->getTotalLength() * 100.0 << "%" << std::endl;
}
bool error = taskProgress->hadError();
if (error == false)
{
isDownloaded = true;
}
else
{
std::cout << "Download failed with code: " << taskProgress->statusCode() << std::endl;
downloadError = true;
fileToWrite.deleteFile();
}
}
else
{
downloadError = true;
fileToWrite.deleteFile();
}
}
