Hi,
I’m testing out iOS builds of my game, and the majority of my problems (so far!) seem to be to do with File IO.
I’ve already fixed problems to do with accessing game files (this thread solved my problem), but now I’m having trouble creating user data files. From everything I’ve read so far, I understand that I should use the “/Documents” folder inside the app. So, here is my code:
string savepath;
if (Application.platform == RuntimePlatform.IPhonePlayer) {
savepath = Application.dataPath.Replace ("/Data", "/Documents/");
} else {
savepath = Application.dataPath;
}
...
var serializer = new XmlSerializer (typeof(Station));
var stream = new FileStream (savepath, FileMode.Create);
serializer.Serialize (stream, this);
stream.Close ();
However, this throws the following DirectoryNotFoundException:
DirectoryNotFoundException: Could not find a part of the path “/var/mobile/Applications/5C190393-F75D-4CA5-89CA-17C8E42EF954/game.app/Documents/station.xml”.
at System.IO.FileStream…ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0
at System.IO.FileStream…ctor (System.String path, FileMode mode) [0x00000] in :0
The path looks as it should to me (the Documents folder should already exist in the app instance, I believe?), but I could be wrong. Any ideas? (I’ve tried FileMode.CreateNew as well as FileMode.Create).
Any help would be appreciated, thanks!
Landern
2
/var/mobile/Applications/5C190393-F75D-4CA5-89CA-17C8E42EF954/game.app/Documents/station.xml"
Shouldn’t it be:
/var/mobile/Applications/5C190393-F75D-4CA5-89CA-17C8E42EF954/Documents/station.xml"
I base this off the apple documentation of the application directory structure found here. Basically you are trying to write to the application bundle.
You can also use:
Application.persistentDataPath
here is some example that can be use to test the saving and loading from ios devices
what i am doing is i ma creating a xml file in ios from a news feed and reading the saved data.
Void Awake()
{
livefileLocation = "http://example.com/news-feeds/";
localfileLocation = Application.dataPath.Replace ("your_app_name_goes_here.app/Data", "/Documents/newfile.xml");
#if UNITY_IPHONE
if (iPhoneSettings.internetReachability == iPhoneNetworkReachability.ReachableViaWiFiNetwork ||iPhoneSettings.internetReachability == iPhoneNetworkReachability.ReachableViaCarrierDataNetwork)
{
StartCoroutine(LoadAndWriteNewsXML(livefileLocation));
}
else
{
read();
}
#endif
}
//load news xml file from net
IEnumerator LoadAndWriteNewsXML(string encodedurl) //load xml file
{
WWW newsxmldata = new WWW (encodedurl);
yield return newsxmldata;
System.IO.File.WriteAllText (localfileLocation,newsxmldata.text);
print ("New Data writen");
read();
}
//read from ios device
void read()
{
if (File.Exists(localfileLocation))
{
FileInfo theSourceFile = new FileInfo (localfileLocation );
StreamReader reader = theSourceFile.OpenText();
string text;
do
{
text = reader.ReadLine();
loadeddata+=text;
// now loadeddata have full content that you whis to get from ios device
} while (text != null);
}
else
{
Debug.LogError("Can't find file");
}
}
Hope this might help someone 
This post solved my saving issue but…
After saving to application persistent data path on iOS, how can I access the file on the actual iPhone?
Thanks in advance!