Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MDarkwing · Jun 11, 2014 at 11:05 AM · iphonesavingfilestream

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;
     }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tim-Michels · Jun 11, 2014 at 11:48 AM 1
Share

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?

avatar image MDarkwing · Jun 11, 2014 at 12:00 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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:

  1. Put the file name WITH "/" in a variable!

  2. Add Environmental variable in Awake.

Thank you Tim

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image almo · Jan 19, 2015 at 06:47 PM 0
Share

Which part of the code? I'd like to know where the error is in the code you posted in the question.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges