React Native Async Storage calls the following method to find an appropriate directory in the Simulator's file system:
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) .firstObject
By opening Finder, it can be observed this evaluates on my machine to ~/Library/Developer/CoreSimulator/Devices/6A36BEDF-71DA-4A9B-9CDD-EBC6BCE11BC0/data/Containers/Data/Application/D258BA62-85FF-4288-95BF-D1318C63B947/Library/Application Support/
I now hope, in my SwiftUI extension widget for the same React Native iOS app, to access this folder. But when I call FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
, this evaluates to /Users/brandon/Library/Developer/CoreSimulator/Devices/6A36BEDF-71DA-4A9B-9CDD-EBC6BCE11BC0/data/Library/Application Support/
As you can see, the results for these are quite different. Why does the Swift method appear to find a different folder and how I can I alter it to access the one the ObjC method finds?
I'm expecting the Swift code to evaluate to the same as that generated by React Native Async Storage in ObjC
The problem is not Swift vs Objective-C. Try the two from within a same app and you will receive the folder.
The problem is app vs widget, each of which has a unique, separate sandbox. You can use an “app group” if you want the app and widget access a shared container.
See Configuring App Groups for information about adding the app group capability in Xcode.
You access this container with containerURLForSecurityApplicationGroupIdentifier:
from Objective-C:
NSURL *baseUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.domain.foo"];
And from Swift, use containerURL(forSecurityApplicationGroupIdentifier:)
:
guard let baseUrl = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.com.domain.foo")
else {
fatalError("app group container not found")
}