Search code examples
iosjsonios-simulatorfilepathswift5

How to read a JSON file in swift 5 (iOS) saved by the simulator at Library/Developer/CoreSimulator/Devices/


I saved a JSON file called abc.json inside my iOS app. It got saved to the following path:

Library/Developer/CoreSimulator/Devices/CB7CE5E3-1178-4A44-804D-6558A937E3CA/data/Containers/Data/Application/1CC01B6D-F5C4-4C20-B370-9E036785EB6C/Documents/abc.json

I reloaded my app and the file path had changed to:

Library/Developer/CoreSimulator/Devices/CB7CE5E3-1178-4A44-804D-6558A937E3CA/data/Containers/Data/Application/2544BD3C-C797-4269-9482-B6E7E0159BA3/Documents/abc.json

Every time I reload the app on my Xcode the path changes. I have 2 questions.

  1. Why does the path keep changing?
  2. How do I read the file (abc.json) saved by the simulator if its path keeps changing?

I tried finding answers for this but did not come across anything that clarified my confusion.


Solution

  • Every time you install the app, it gets a new UUID. That's normal. Most commonly, you'd access it by just printing out the path during launch, but another convenient mechanism is to use simctl:

    xcrun simctl get_app_container booted <bundle> data
    

    "booted" here is a convenient way to access the currently running simulator, but you can also pass an explicit UUID to identify the simulator (CB7CE5E3-1178-4A44-804D-6558A937E3CA in your example). The "data" parameter returns the data container, which is often what you want. The default is the app container.