Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions native/ios/WatermelonDB/DatabasePlatformIOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@ void initializeSqlite() {
}

std::string resolveDatabasePath(std::string path) {
#if TARGET_OS_MACCATALYST
// On Mac Catalyst (unsandboxed), NSDocumentDirectory resolves to
// ~/Documents and any access there triggers the macOS TCC prompt
// asking the user to grant access to their Documents folder at
// launch. Use Application Support instead — per-app, private to the
// bundle, and never triggers TCC.
//
// We intentionally don't migrate from the legacy ~/Documents path:
// probing for a legacy file would itself trigger the TCC prompt
// we're trying to avoid.
NSError *err = nil;
NSURL *appSupport = [NSFileManager.defaultManager URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:&err];

if (err) {
NSLog(@"Error: %@", err);
throw std::runtime_error("Failed to resolve database path - could not find Application Support URL");
}

NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier] ?: @"WatermelonDB";
NSURL *dir = [appSupport URLByAppendingPathComponent:bundleId isDirectory:YES];
[NSFileManager.defaultManager createDirectoryAtURL:dir
withIntermediateDirectories:YES
attributes:nil
error:nil];
Comment on lines +46 to +49
NSString *dbPath = [dir URLByAppendingPathComponent:
[NSString stringWithFormat:@"%s.db", path.c_str()]].path;

return std::string([dbPath cStringUsingEncoding:NSUTF8StringEncoding]);
#else
// Default: app documents/<name>.db
NSError *err = nil;
NSURL *documentsUrl = [NSFileManager.defaultManager URLForDirectory:NSDocumentDirectory
Expand All @@ -36,6 +69,7 @@ void initializeSqlite() {
[NSString stringWithFormat:@"%s.db", path.c_str()]].path;

return std::string([dbPath cStringUsingEncoding:NSUTF8StringEncoding]);
#endif
}

void deleteDatabaseFile(std::string path, bool warnIfDoesNotExist) {
Expand Down
24 changes: 24 additions & 0 deletions native/ios/WatermelonDB/objc/WMDatabaseDriver.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,37 @@ - (NSString *) pathForName:(NSString *)dbName
if ([dbName hasPrefix:@"file:"] || [dbName containsString:@"/"]) {
return dbName;
} else {
#if TARGET_OS_MACCATALYST
// On Mac Catalyst (unsandboxed), NSDocumentDirectory resolves to
// ~/Documents and any access there triggers the macOS TCC prompt
// asking the user to grant access to their Documents folder at
// launch. Use Application Support instead — per-app, private to
// the bundle, and never triggers TCC.
//
// We intentionally don't migrate from the legacy ~/Documents path:
// probing for a legacy file would itself trigger the TCC prompt
// we're trying to avoid.
NSURL *appSupport = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:nil];
NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier] ?: @"WatermelonDB";
NSURL *dir = [appSupport URLByAppendingPathComponent:bundleId isDirectory:YES];
[[NSFileManager defaultManager] createDirectoryAtURL:dir
withIntermediateDirectories:YES
attributes:nil
error:nil];
Comment on lines +38 to +48
return [[dir URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.db", dbName]] path];
#else
NSURL *url = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:false
error:nil];

return [[url URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.db", dbName]] path];
#endif
}
}

Expand Down
Loading