Search code examples
iosswiftobjective-creact-nativeasyncstorage

Why does Swift FileManager.default.urls(...) return a different result than ObjC implementation (iOS simulator)?


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


Solution

  • 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.