From b6ab931bcf17d1b30f93a77818afd8b5ab67ab76 Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 25 Aug 2021 18:51:03 +0100 Subject: [PATCH] File: Add helper function to locate shared containers on macOS and iOS File::getContainerForSecurityApplicationGroupIdentifier will return the path to a container which is shared between all apps using the specified app group ID. This might be useful if you need to share resources between a standalone app and an AUv3 plugin, for example. --- modules/juce_core/files/juce_File.h | 11 +++++++++++ modules/juce_core/native/juce_mac_Files.mm | 8 ++++++++ modules/juce_core/native/juce_mac_Network.mm | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/modules/juce_core/files/juce_File.h b/modules/juce_core/files/juce_File.h index 17ccaba7c4..9c7cf115ff 100644 --- a/modules/juce_core/files/juce_File.h +++ b/modules/juce_core/files/juce_File.h @@ -1074,6 +1074,17 @@ public: void addToDock() const; #endif + #if JUCE_MAC || JUCE_IOS + /** Returns the path to the container shared by all apps with the provided app group ID. + + You *must* pass one of the app group IDs listed in your app's entitlements file. + + On failure, this function may return a non-existent file, so you should check + that the path exists and is writable before trying to use it. + */ + static File getContainerForSecurityApplicationGroupIdentifier (const String& appGroup); + #endif + //============================================================================== /** Comparator for files */ struct NaturalFileComparator diff --git a/modules/juce_core/native/juce_mac_Files.mm b/modules/juce_core/native/juce_mac_Files.mm index 1a4d075165..029d34157a 100644 --- a/modules/juce_core/native/juce_mac_Files.mm +++ b/modules/juce_core/native/juce_mac_Files.mm @@ -506,4 +506,12 @@ void File::addToDock() const } #endif +File File::getContainerForSecurityApplicationGroupIdentifier (const String& appGroup) +{ + if (auto* url = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: juceStringToNS (appGroup)]) + return File (nsStringToJuce ([url path])); + + return File(); +} + } // namespace juce diff --git a/modules/juce_core/native/juce_mac_Network.mm b/modules/juce_core/native/juce_mac_Network.mm index f1ede14844..bf87acc0ee 100644 --- a/modules/juce_core/native/juce_mac_Network.mm +++ b/modules/juce_core/native/juce_mac_Network.mm @@ -518,7 +518,7 @@ struct BackgroundDownloadTask : public URL::DownloadTask void didFinishDownloadingToURL (NSURL* location) { - NSFileManager* fileManager = [[NSFileManager alloc] init]; + auto* fileManager = [NSFileManager defaultManager]; error = ([fileManager moveItemAtURL: location toURL: createNSURLFromFile (targetLocation) error: nil] == NO);