diff --git a/package/cpp/MmkvHostObject.cpp b/package/cpp/MmkvHostObject.cpp index 2d4b582b..2ed9e9a9 100644 --- a/package/cpp/MmkvHostObject.cpp +++ b/package/cpp/MmkvHostObject.cpp @@ -12,6 +12,9 @@ #include #include #include +#ifdef __APPLE__ +#include +#endif using namespace mmkv; using namespace facebook; @@ -28,7 +31,21 @@ MmkvHostObject::MmkvHostObject(const facebook::react::MMKVConfig& config) { MMKVMode mode = getMMKVMode(config); #ifdef __APPLE__ - instance = MMKV::mmkvWithID(config.id, mode, encryptionKeyPtr, pathPtr); + NSString* appGroup = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"AppGroup"]; + if (appGroup != nil) { + MmkvLogger::log("RNMMKV", "Info.plist specifies a custom AppGroup - ignoring `path`..."); + NSString* groupDir = + [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:appGroup] + .path; + if (groupDir == nil) { + throw std::runtime_error( + "Failed to create MMKV instance! Custom App Group directory not accessible!"); + } + std::string groupDirStr = groupDir.UTF8String; + instance = MMKV::mmkvWithID(config.id, mode, encryptionKeyPtr, &groupDirStr); + } else { + instance = MMKV::mmkvWithID(config.id, mode, encryptionKeyPtr, pathPtr); + } #else instance = MMKV::mmkvWithID(config.id, DEFAULT_MMAP_SIZE, mode, encryptionKeyPtr, pathPtr); #endif