- Home /
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.
Please give me some suggestion what changes I need to do to make this correct.
Access Denied!
Apparently some iOS devices have trouble with file read and write due to protected filesystems.
'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.
@meat5000, thanks for your reply. what about file writing?
@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.
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.
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
Answer by siddharth3322 · May 07, 2016 at 12:18 PM
Thanks to Maximilian Gerhardt for giving me this answer.
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 ();
}