- Home /
How to read local XML file on iOS?
I wanted to read a locally stored XML file.
This is the snippet of my simple code, just to test the functionality:
public class testLoadXML : MonoBehaviour
{
string debugText;
void Start ()
{
debugText = "path = ";
StartCoroutine(parseXML());
}
void OnGUI()
{
GUI.Label(new Rect(0, 30, Screen.width, Screen.height), debugText);
}
IEnumerator parseXML()
{
string path;
if(Application.isEditor)
path = "file://" + Application.dataPath + "/../../testXML/interface/test.xml";
else
path = "file://" + Application.dataPath + "/../test.xml";
debugText += path;
if(System.IO.File.Exists(Application.dataPath + "/../test.xml"))
debugText += "; file exists = yes";
else
debugText += "; file exists = no";
debugText += "; result = ";
var wwwDownload = new WWW(path);
while(!wwwDownload.isDone)
yield return null;
if(!string.IsNullOrEmpty(wwwDownload.error))
debugText += wwwDownload.error;
else
debugText += wwwDownload.text;
}
}
The process of the build is:
1. I build the xcode project in unity with Simulator SDK on the player setting.
2. On xcode on the target build phases I add the XML file on the Copy Files phase, with destination : Resources.
3. Then I build and run the project on the iPad simulator.
Now the problem is:
When I run the project on the Unity editor, everything runs fine and the debugText would contains the text from the XML file.
But when I run it on the iPad simulator, eventhough the File.Exists would return true, the wwwDownload.error will always return BAD URL.
Does anybody know why it returned BAD URL while File.Exists is true?
Thanks!
Answer by steakpinball · Feb 28, 2013 at 10:09 AM
WWW is not needed on iOS platforms to read a local file. FileStream can be used to read and write local files.
Thank you very very much! It worked like a charm! Is this documented anywhere on the unity3d docs that we don't need WWW to read local file on iOS platform?
I don't believe it is specifically. The closest it comes is the list of .NET classes available with selected build platforms/optimization levels.
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoCompatibility.html