Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by siddharth3322 · May 07, 2016 at 10:03 AM · androidiosfileapplefile-io

File Read-Write Error in iOS

I have used following code for file reading and writing.

 private void StorePuzzleData ()
     {
         FileInfo fileInfo = new FileInfo (Application.persistentDataPath + "\\" + difficultyLevel + puzzleId + ".txt");
 
         if (fileInfo.Exists)
             fileInfo.Delete ();
 
         string fileData = string.Empty;
 
         foreach (CellInformation cellInfo in cellInfoList)
             fileData += cellInfo.RowIndex + "#" + cellInfo.ColIndex + "#" + cellInfo.number + "#" + cellInfo.CellColor + "#" + cellInfo.CellDisplayColor + "#" + (cellInfo.IsGroupComplete ? 1 : 0) + ",";
 
         StreamWriter streamWriter = fileInfo.CreateText ();
         streamWriter.WriteLine (fileData);
         streamWriter.Close ();
 
         DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime);
 
     }
 
     private void ReadPuzzleData ()
     {
         // format:  rownumber, colnumber, number, cellcolor, celldisplaycolor, isgroupcomplete
 
         StreamReader streamReader = File.OpenText (Application.persistentDataPath + "\\" + difficultyLevel + puzzleId + ".txt");
         string fileData = streamReader.ReadLine ();
     }

But I am getting following error in actual iOS device running. This code working correct in iMac as well in android device.

alt text

alt text

Please give me some suggestion what changes I need to do to make this correct.

screen-shot-2016-05-07-at-33013-pm.png (78.1 kB)
screen-shot-2016-05-07-at-33026-pm.png (90.9 kB)
Comment
Add comment · Show 10
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 meat5000 ♦ · May 07, 2016 at 10:10 AM 0
Share

Access Denied!

Apparently some iOS devices have trouble with file read and write due to protected filesystems.

https://www.google.co.uk/search?q=unity+ios+access+denied&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=5b0tV86iEYOy7Aap45ywDg&gws_rd=ssl

'Jailbroken' phones apparently dont have this problem.

Perhaps do a try/catch on your File.Open call and if it errs use File.OpenRead ins$$anonymous$$d. This should allow full access on Jailbroken devices but allow read capability on those that are protected.

The other answer I got from the search is to only work under the documents folder as it allows fuller access by default.

avatar image siddharth3322 meat5000 ♦ · May 07, 2016 at 10:13 AM 0
Share

@meat5000, thanks for your reply. what about file writing?

avatar image siddharth3322 meat5000 ♦ · May 07, 2016 at 10:19 AM 0
Share

@meat5000, basically I am writing puzzle data to file and at next time reading data from file for my puzzle game. So that game player can start game from where he left last time.

avatar image meat5000 ♦ siddharth3322 · May 07, 2016 at 11:13 AM 0
Share

Well, first there are a few things you should try, like tweaking your file path.

I notice your Documents\debutant1.txt has a backslash ins$$anonymous$$d of a forward one. Also I don't know how important it is but I'd usually parse my data to String first before building a filepath or using it in a Debug msg.

Show more comments
avatar image sandeepsmartest · May 07, 2016 at 12:24 PM 0
Share

Hi , Below given code may help you to READ the text file.

 public void ReadFile( )
        {
          //TextAss declared as public variable and drag dropped the text file in inspector
              string content = TxtAss.text;
              print(TxtAss.text.Length);
         }

Hope this may help you at a very basic level. Nsks

1 Reply

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

Answer by siddharth3322 · May 07, 2016 at 12:18 PM

Thanks to Maximilian Gerhardt for giving me this answer.

File Read-Write Error in iOS

The correct way to always generate the correct path is to use the Path.Combine(string, string) function. This will combine two paths using the correct directory path seperator, which can also be seperatly accessed through Path.DirectorySeparatorChar.

 using System.IO; /* must be imported */
 
  private void StorePuzzleData ()
  {
      FileInfo fileInfo = new FileInfo (Path.Combine(Application.persistentDataPath, difficultyLevel + puzzleId + ".txt"));
 
      if (fileInfo.Exists)
          fileInfo.Delete ();
 
      string fileData = string.Empty;
 
      foreach (CellInformation cellInfo in cellInfoList)
          fileData += cellInfo.RowIndex + "#" + cellInfo.ColIndex + "#" + cellInfo.number + "#" + cellInfo.CellColor + "#" + cellInfo.CellDisplayColor + "#" + (cellInfo.IsGroupComplete ? 1 : 0) + ",";
 
      StreamWriter streamWriter = fileInfo.CreateText ();
      streamWriter.WriteLine (fileData);
      streamWriter.Close ();
 
      DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime);
 
  }
 
  private void ReadPuzzleData ()
  {
      // format:  rownumber, colnumber, number, cellcolor, celldisplaycolor, isgroupcomplete
 
      StreamReader streamReader = File.OpenText (Path.Combine(Application.persistentDataPath, difficultyLevel + puzzleId + ".txt"));
      string fileData = streamReader.ReadLine ();
  }


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

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

Related Questions

Upload/Download user files Android/IOS, 0 Answers

Load and save file. IOS problem 0 Answers

Is it possible to get my Android app on iOS devices without having to rewrite all of my code in Swift or Objective-C? 0 Answers

Difference between Application.persistantDataPath Vs Application.dataPath 2 Answers

How to tell android to open a file 0 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