Search Unity

Reading/Saving JSON file on iOS issues

Discussion in 'iOS and tvOS' started by coshea, Jul 3, 2019.

  1. coshea

    coshea

    Joined:
    Dec 20, 2012
    Posts:
    319
    Hi

    Following this tutorial, I need to read and write to JSON files on mobile...
    https://learn.unity.com/tutorial/live-session-quiz-game-2

    Tutorial says to put files in StreamingAssets folder.

    This works fine for me in the editor (with the file in StreamingAssets)...

    Code (csharp):
    1.  
    2. string filePath = Path.Combine(Application.streamingAssetsPath, "myfile.json");
    3. if (File.Exists(filePath)){
    4.             string dataAsJson = File.ReadAllText(filePath);
    5.              myList = JsonUtility.FromJson<WrapperData>(dataAsJson);
    6. }
    7.  
    But on device I'm getting a file issue:

    nauthorizedAccessException: Access to the path "/var/containers/Bundle/Application/[appid]/myapp.app/Data/Raw/myfile.json" is denied.

    at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions


    What am I doing wrong here?

    Should I be using Resources instead of StreamingAssets?

    Thanks