When I try to use System.IO to write a text file to iPhone, it shows error Access to path “/xxx/xxx” denied. How can I solve it?
The implication is that you’re writing to a path constructed with Application.dataPath. That’s the game assets folder, and could be overwritten when installing an update. Instead, use Application.persistentDataPath Unity - Scripting API: Application.persistentDataPath. You have permissions for that folder, and it won’t be destroyed on update.
it is not that easy to read or write file in iphone we have to give the specific path in order to read or write.
here is the sample code
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
public static class GameManager
{
private static string path;
public static void starting ()
{
path = Application.dataPath.Substring (0, Application.dataPath.Length - 20 )+"Documents";
}
//---------------------file handleres------------------------------
public static void writeToFile(string filename , int value)
{
StreamWriter sw = new StreamWriter(path+"/"+filename);
string temp = value.ToString();
sw.Write("");
sw.WriteLine(temp);
sw.Close();
}
public static int ReadFromFile(string filename )
{
StreamReader sr = new StreamReader(path+"/"+filename);
string temp = sr.ReadLine();
int anothertemp = int.Parse(temp);
sr.Close();
return anothertemp;
}
//---------------------file handleres------------------------------
}
i think it will work . let me now if any problem.