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かどうかはチェックしてないのね。
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で指定されているものと同じで正しい場合には、ディレクトリは必ず作成されている。