Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Application.persistentDataPath issues on iOS Device

Discussion in 'iOS and tvOS' started by KahunaCoder, Mar 25, 2014.

  1. KahunaCoder

    KahunaCoder

    Joined:
    Feb 10, 2014
    Posts:
    10
    Hello everyone,

    I have been wrestling with a file access problem when writing to Application.persistentDataPath when running on an iOS device. I've tried to search feverishly both here and on Google and I have tried many solutions / suggestions but still get the same error. Here's the bit of code that I'm using to serialize and write an xml file:

    Code (csharp):
    1. public static bool SerializeObject<T>(string filename, T data)
    2.     {
    3.         string text="";
    4.  
    5.         try
    6.         {
    7.             XmlSerializer serializer = new XmlSerializer(typeof(T));
    8.            
    9.             using(var stream = new FileStream(Path.Combine(Application.persistentDataPath, filename), FileMode.Create, FileAccess.Write))
    10.             {
    11.                 serializer.Serialize(stream, data);
    12.                 stream.Close();
    13.             }
    14.         }
    15.         catch (System.Exception err)
    16.         {
    17.             text = err.ToString();
    18.             inst.ShowErrorMsg(text);
    19.             return false;
    20.         }
    21.  
    22.         return true;
    23.     }
    And this is how I call it:

    Code (csharp):
    1. SerializeObject<SaveData>(Application.persistentDataPath + "\\" + saveFileName, tempData)
    I get the unauthorizedexception:access .... is denied at System.IO.FileStream error. Any help would be tremendously appreciated!

    Thanks!
     
  2. KahunaCoder

    KahunaCoder

    Joined:
    Feb 10, 2014
    Posts:
    10
    Hrm...nevermind. I think I may have found my problem! Testing out the fix right now.

    EDIT: Okay, confirmed fixed! The problem was, I was how I was passing in the "filename" parameter into SerializedObject. Simplifying my call to:

    Code (csharp):
    1. SerializeObject<SaveData>(saveFileName, tempData)
     
    Last edited: Mar 25, 2014
unityunity