- Home /
FileStream + BinaryFormatter from C# to iOS doesn't work?
I'm trying to serialze an object using FileStream and BinaryFormatter technique. It works quite well on Android but on iOS it shows up the error. I've been rumblin with this problem for a week now read bunch of documentation and examples and tried to implement that all but nothing seems to work. I'm afraid this is beyond the scope of my current programing ability.
The error I get in XCode is following
Runned UnityEngine.Debug:Internal_Log(Int32, String, Object) UnityEngine.Debug:Log(Object) TestStore:Update()
(Filename: /Applications/buildAgent/work/d3d49558e4d408f4/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 53)
UnauthorizedAccessException: Access to the path "/var/mobile/Applications/A622D2E1-7364-48AD-91A4-053BA88A97C7/DocumentsTestSejva.svj" is denied. 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, FileAccess access, FileShare share, Int32 bufferSize) [0x00000] in :0 at System.IO.File.Create (System.String path, Int32 bufferSize) [0x00000] in :0 at System.IO.File.Create (System.String path) [0x00000] in :0 at TestStore.SaveDataAndroid () [0x00000] in :0 at TestStore.Update () [0x00000] in :0
(Filename: Line: -1)
I'm guessing that the either FileStream or BinaryFormatter class is not compatibile with iOS. I have an application that is on store and is using same techinque which I made last year and it worked although I was saving data to a text file rather than serializing object because it was just some options parameters. Now I have to serialize class because data on this game I'm developing is quite more complex. Formerly I've used hardcoded pathing but now as I've read im using Application.persistantDataPath and also I've seen that due to reflection and something aobut FileStream doing at runtime which is not compatible with iOS, that I need to set some environment variable like this which I also did.
void Awake()
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
}
}
If anyone can help me with this problem it would be most appreciated.
Thank you in advance
EDIT: Adding methods for saving and reading data: public void SaveData() { if (!File.Exists(Application.persistentDataPath + "/TestSejva.svj")) File.Create(Application.persistentDataPath + "/TestSejva.svj");
FileStream fs = new FileStream(Application.persistentDataPath + "TestSejva.svj", FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
BinaryFormatter bf = new BinaryFormatter();
Sranje sr = new Sranje();
sr.FillObject(Random.Range(5, 15));
try
{
bf.Serialize(fs, sr);
Label1.text = "Data saved";
}
catch (System.Exception e)
{
Label1.text = e.ToString();
}
fs.Close();
fs.Dispose();
}
public void LoadData()
{
FileStream fs = new FileStream(Application.persistentDataPath + "/TestSejva.svj", FileMode.Open, FileAccess.Read, FileShare.None);
BinaryFormatter bf = new BinaryFormatter();
Sranje sr = (Sranje)bf.Deserialize(fs);
fs.Close();
string rez = "";
for (int i = 0; i < sr.Randomi.Length; i++)
{
rez += sr.Randomi[i].ToString();
}
rez += (" " + sr.Bla);
Label1.text = rez;
}
I also use this technique to serialize objects on iOS, just make sure there's a '/' after you Application.persistantDataPath and I would set the Environment variable in a 'Start'-function, since its only neccesary to set it once.
There's nothing wrong with the way you serialize your objects (using Filestream and BinaryFormatter), but maybe you're trying to serialize an object which can't. Are there any exotic datatypes you're trying to serialize?
The class has string and int[] as types. I made sure there is a "/" in the file name. Also start and awake are called once so i think it doesnt matter which one is used first, but Awake is called before Start therefore i expect that's why they put it there.
No success. I've edited the code to expose methods I've created for this.
EDIT Dumb me I see that I've missed one "/", that's what happens when u hardcode path. I'll check again with the fix of that and let know if this works or not.
Answer by MDarkwing · Jun 11, 2014 at 04:20 PM
Yeah Tim was right, I've missed the "/" in one part of the code and it's the problem.
The lesson is:
Put the file name WITH "/" in a variable!
Add Environmental variable in Awake.
Thank you Tim
Which part of the code? I'd like to know where the error is in the code you posted in the question.
Answer by WingSoft · Jan 28, 2015 at 05:35 PM
Just for the record >>>>
If you try adding the code solution and think it didn't help, you MUST uninstall the app from your device.
That's because of the data corruption on the file you attempt to create in the first place.
I had the issue, and that solve the problem.
Your answer
Follow this Question
Related Questions
File saving error 1 Answer
Saving game question. 1 Answer
Saving screenshot to iPhone Camera Roll 3 Answers
FileStream in IPhone. 1 Answer
Unity iPhone: Can I store data on the iPhone to be used later? 4 Answers