1

I am trying to write to a XML on iOS, but it seems to be impossible. Tried several different this but none have worked.

I have read that iOS only have write access to it´s /Documents folder, so I have tried to set a Application.dataPath to the Documents folder. But I get this error in Xcode :

DirectoryNotFoundException: Could not find a part of the path "/Users/mariusmathisen/Library/Application Support/iPhone       Simulator/7.1/Applications/6AE4AF70-EFDF-46F0-9947-5C0936F3AD27/DaXml.app/Data/Documents/gamexmldata.txt".

This is my code to write to XML :

lic void WriteToXml()
{
   Debug.Log ("Du nådde metoden");
   //string filepath = Application.dataPath + @"/Data/gamexmldata.xml";

   XmlDocument xmlDoc = new XmlDocument();


   GameControl.control.outputXml = "Du nådde WriteXML";
   if(File.Exists(Application.dataPath + "/Documents/gamexmldata.txt"))
   {
     Debug.Log("Filen Fantes");
     GameControl.control.outputXml = "DU ER I Writetoxml og filen fantes";



     xmlDoc.Load(Application.dataPath + "/Documents/gamexmldata.txt");
     //xmlDoc.Load(filepath);

     XmlElement elmRoot = xmlDoc.DocumentElement;

     // elmRoot.RemoveAll(); // remove all inside the transforms node.

     XmlElement elmNew = xmlDoc.CreateElement("rotation"); // create the rotation node.

     XmlElement rotation_X = xmlDoc.CreateElement("x"); // create the x node.
     rotation_X.InnerText = x; // apply to the node text the values of the variable.

     XmlElement rotation_Y = xmlDoc.CreateElement("y"); // create the y node.
     rotation_Y.InnerText = y; // apply to the node text the values of the variable.

     XmlElement rotation_Z = xmlDoc.CreateElement("z"); // create the z node.
     rotation_Z.InnerText = z; // apply to the node text the values of the variable.
     XmlElement navnElement = xmlDoc.CreateElement("name");
     navnElement.InnerText = navn;

     GameControl.control.inputXml = navnElement.InnerText;

     elmNew.AppendChild(rotation_X); // make the rotation node the parent.
     elmNew.AppendChild(rotation_Y); // make the rotation node the parent.
     elmNew.AppendChild(rotation_Z); // make the rotation node the parent.
     elmNew.AppendChild(navnElement);
     elmRoot.AppendChild(elmNew); // make the transform node the parent.

     xmlDoc.Save(Application.dataPath + "/Documents/gamexmldata.txt"); // save file.
     Debug.Log("Filen er lagret");
   }else{

     xmlDoc.Save(Application.dataPath + "/Documents/gamexmldata.txt");
   }
}

Are there any suggestion to why I can´t get the XML to write to iOS and it´s folder? What am I doing wrong?

Praise
  • 557
  • 2
  • 8
  • 23
  • It now works and on iOS Simulator it both reads and writes the XML file, but when I test on device I get this error : XmlException: Document element did not appear. file:///var/mobile/Applications/47C26549-9CB0-46C2-9906-66399A5E515C/Documents/gamexmldata.txt Line 1, position 1. – Praise Mar 14 '14 at 11:40

2 Answers2

0

You should be able to save to disk by using the Application.persistentDataPath. It goes to a public folder and does not get deleted when the app is updated. Link to unity docs.

Esa
  • 1,636
  • 3
  • 19
  • 23
0

I think that the problem lies in the fact that your path is not completely correct.

According to Apple you are required to write the file into:

/Documents/gamexmldata.txt

Instead you are writing it as:

/[AppName].app/Documents/gamexmldata.txt

To solve this you can use the following:

fileSaveLocation = Application.dataPath.Replace( "/[AppName].app/Data", "/Documents/gamexmldata.txt" );

And to make sure - [AppName] here is the development app Id name you selected when you build the application (not the 'name' of the application, but it's Id name with Apple).

Documentation about the required location by Apple can be found here:

https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

Let me know if it helped.

Adi
  • 2,033
  • 5
  • 23
  • 26