Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by notashark · Nov 26, 2012 at 09:07 PM · iossavingdata

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!

Comment
Add comment
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

4 Replies

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

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.

Comment
Add comment · Show 4 · 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 notashark · Nov 26, 2012 at 09:40 PM 0
Share

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!

avatar image Landern · Nov 26, 2012 at 09:40 PM 0
Share

Glad to hear you have it working :)

avatar image vaibhav.barman · Jul 02, 2014 at 05:07 AM 0
Share

$$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 ! :)

avatar image elliselkins · Nov 29, 2016 at 05:31 PM 1
Share

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 ();

avatar image
3

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

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
avatar image
2

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 :)

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 Atulee · Apr 20, 2018 at 03:00 PM 0
Share

Did It Worked?

avatar image
0

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!

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

17 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

Related Questions

The best way to save and load data for mobile in unity ? 1 Answer

iOS Streaming Assets inaccessible 0 Answers

One save for a free and premium version of the game on iOS 0 Answers

Unity Webplayer save global data 0 Answers

Saving object locations after game close 1 Answer


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