- Home /
Saving data files in iOS
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!
Answer by Landern · Nov 26, 2012 at 09:30 PM
/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.
Oh, you're right, thanks! I read that documentation about ten times, each time thinking the Documents folder was somehow under the *.app file. I've now changed the code to:
savepath = Application.dataPath.Replace ("game.app/Data", "/Documents/");
Thanks for the help!
$$anonymous$$uch thanks .. been trying to figure this out since last 3 days .. Went through your post and yes, it finally did help ! Works like magic now ! :)
This didn't work for me, maybe they've changed the path since this answer was given. I used persistentDataPath like @$$anonymous$$ $$anonymous$$ehrer said below, and that worked for me.
string filePath = Application.persistentDataPath + "/testFile.txt";
StreamWriter sr = File.CreateText (filePath);
sr.WriteLine ("This is my file.");
sr.WriteLine ("I can write ints {0} or floats {1}, and so on.", 1, 4.2);
sr.Close ();
Answer by Brian-Kehrer · Jan 11, 2015 at 06:10 PM
You can also use: Application.persistentDataPath
http://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
Answer by abi-kr01 · Jul 04, 2014 at 05:34 AM
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 :)
Answer by mraeclo · Dec 05, 2021 at 07:53 AM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613084533im_/https://answers.unity.com/themes/thub/images/avi.jpg)