Json not saving on iOS

So i’m using json to save some data, and on pc it works completly fine, but on iOS it dosent apear to do anything.

This is the script I use to save it

        string saveText = JsonUtility.ToJson ( new BuildInfo( build.width, build.height, build.idList) ); // Converts into json string
        File.WriteAllText (Application.dataPath + "/IdList.txt", saveText); // Writes it into the file

        SceneManager.LoadScene("Scene", LoadSceneMode.Single); // <-- this line never executes on ios, so error must be above

This is what the json file looks like on pc ( I can’t access the file on iOS so no idea how it looks there…)

{"width":9,"height":19,"idList":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}

And this is the script I use to load, so far I have no idea if it works, because well, there is nothing to read

        string loadedText = File.ReadAllText (Application.dataPath + "/IdList.txt");
        BuildInfo loadedBuildInfo = JsonUtility.FromJson<BuildInfo>(loadedText);

The docs of Application.dataPath should explain it: Unity - Scripting API: Application.dataPath

“iOS player: <path to player app bundle>/<AppName.app>/Data (this folder is read only, use Application.persistentDataPath to save data).”

Thanks!

The Application.persistentDataPath didn’t work on pc, but it appears to be working on iOS, so I just did this:

#if UNITY_IOS
         File.WriteAllText (Application.persistentDataPath + "/IdList.txt", saveText); // Writes it into the file ( iOs version)
#else
        File.WriteAllText (Application.dataPath + "/IdList.txt", saveText); // Writes it into the file
#endif
2 Likes