0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

juce::File::getContainerForSecurityApplicationGroupIdentifier()の挙動

Last updated at Posted at 2025-07-13

Standalpne AppとAUv3で同じ設定を使うのに、App Group IDを作ってそれを指定することはよくあると思う。JUCEにはApp Group IDを渡してコンテナのディレクトリを取得するgetContainerForSecurityApplicationGroupIdentifier()というメソッドがFileクラスにある (iOS/Macのみ)。これの挙動を少し調べた。

有効なAPP_GROUP_IDを渡した場合
auto settingsDir = juce::File::getContainerForSecurityApplicationGroupIdentifier(APP_GROUP_ID);
結果:
"/Users/ring2/Library/Group Containers/group.tokyo.studio-r.r2juce.examples.clouddoc"

空文字列を渡した場合
auto settingsDir = juce::File::getContainerForSecurityApplicationGroupIdentifier("");
結果:
"" (空文字列)

無効なAPP_GROUP_IDを渡した場合
auto settingsDir = juce::File::getContainerForSecurityApplicationGroupIdentifier("ABC");
結果:
"/Users/ring2/Library/Group Containers/ABC"

実際の実装は下記のようにラッピングしてるだけなので、これAppleのiOS/macOSの実装がそうなってるだけなのか。有効なApp Group IDかどうかはチェックしてないのね。

juce_Files_mac.mm
File File::getContainerForSecurityApplicationGroupIdentifier (const String& appGroup)
{
    if (auto* url = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: juceStringToNS (appGroup)])
        return File (nsStringToJuce ([url path]));

    return File();
}

なので、実際に自分でこれを使う時には下記のようにApp Groupのコンテナがない時はjassertfalseしつつフォールバックとしてuserApplicationDataDirectoryを取得するようにしてる。

    auto settingsDir = juce::File::getContainerForSecurityApplicationGroupIdentifier(APP_GROUP_ID);
    // If the container is not valid, fall back to the standard location.
    if (!settingsDir.isDirectory()) {
        //  Specified App Group ID is invalid. You should check if you specified the App Group ID in Projucer/Xcode.
        jassertfalse;
        settingsDir =
            juce::File::getSpecialLocation(juce::File::userApplicationDataDirectory)
            .getChildFile("MyApp");
    }

ちなみに、App Groupを設定することでそのディレクトリは自動作成されるし、削除しても次のそのアプリが起動する時には自動的に作成される。JUCEの場合、

auto appGroupDir = juce::File::getContainerForSecurityApplicationGroupIdentifier(APP_GROUP_ID);

を呼び出した後、Entitlementが正しく設定されていて、APP_GROUP_IDで指定する文字列がEntitlementで指定されているものと同じで正しい場合には、ディレクトリは必ず作成されている。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?