Saving or Reading text file works on Mac but not on iOS

The title says it all, the saving and/or the reading of a text file named “points.txt” doesn’t work as expected on iOS but work normally when I test the game on my Mac. I’ve nothing more to says. `

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class PlesaPointSystem : MonoBehaviour {

	public InputField inputf;
	public Button button;
	public RectTransform messageTr;

	public Text DebugInfo;

	public List<Vector2> Points;
	public List<Transform> PointsTransform;
	public List<string> PointsName;

	public GameObject OriginalPoint;
	public AudioSource asource;
	public AudioClip sucessSound;

	int NearestPointIndex = 0;
	byte LOADINTERVAL = 0;
	byte SOUNDUPDATE = 0;

	const float PixelToMap = 0.01953125f;

	public GoogleMap gm;

	public void EndPointCreation () {
		if(string.IsNullOrEmpty(inputf.text)) {
			inputf.gameObject.SetActive(false);
			button.gameObject.SetActive(true);
		} else {
			Points.Add(new Vector2(Input.location.lastData.longitude,Input.location.lastData.latitude));
			PointsTransform.Add(((GameObject)Instantiate(OriginalPoint, new Vector3(0,0,0), Quaternion.identity)).transform);
			PointsName.Add(inputf.text.Replace(",",""));

			float scale = 1 << gm.zoom;
			Vector2 worldCoordinate; // = projection(new Vector2(0,0));
			Vector2 pixelCoordinate;
			Vector2 playerWorldCoordinate = projection(gm.LatLong);
			Vector2 playerPixelCoordinate = new Vector2(Mathf.Floor(playerWorldCoordinate.x * scale),Mathf.Floor(playerWorldCoordinate.y * scale));

			for(int i = 0; i < Points.Count; i++) {
				worldCoordinate = projection(Points.ToArray()*);*

_ pixelCoordinate = new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));_

  •  		pixelCoordinate = new Vector2(pixelCoordinate.x-playerPixelCoordinate.x, pixelCoordinate.y-playerPixelCoordinate.y);*
    

PointsTransform.ToArray().position = new Vector3(pixelCoordinate.xPixelToMap, pixelCoordinate.yPixelToMap,-0.1f);
* }*

* inputf.gameObject.SetActive(false);*
* button.gameObject.SetActive(true);*

* Save();*
* }*
* }*

* public void CreateAPoint () {*
* button.gameObject.SetActive(false);*
* inputf.gameObject.SetActive(true);*
* inputf.text = “”;*
* }*

* public void Save () {*
* StreamWriter sw = null;*
* if(File.Exists(Application.persistentDataPath+“/points.txt”)) {*
* sw = File.CreateText(Application.persistentDataPath+“/points.txt”);*
* }*
* if(sw!=null) {*
* sw.Close();*
* }*
* string Value = “”;*
* for(int i = 0; i < Points.Count; i++) {*
* if(i!=0) {*
* Value+=“,”;*
* }*
Value += “(” + Points.ToArray().x + “:” + Points.ToArray().x + “);”+PointsName.ToArray();
* }*
* File.WriteAllText(Application.persistentDataPath+“/points.txt”, Value);*
* }*

* public void Load () {*
* //DebugInfo.text = Application.persistentDataPath.ToString();*
* Points.Clear();*
* PointsName.Clear();*
* foreach(RectTransform rt in PointsTransform) {*
* Destroy(rt.gameObject);*
* }*
* PointsTransform.Clear();*

* float scale = 1 << gm.zoom;*
* Vector2 worldCoordinate; // = projection(new Vector2(0,0));*
_ Vector2 pixelCoordinate; //= new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));_

* Vector2 playerWorldCoordinate = projection(gm.LatLong);*
_ Vector2 playerPixelCoordinate = new Vector2(Mathf.Floor(playerWorldCoordinate.x * scale),Mathf.Floor(playerWorldCoordinate.y * scale));_

* if(File.Exists(Application.persistentDataPath+“/points.txt”)) {*
* string Value = File.ReadAllText(Application.persistentDataPath+“/points.txt”);*
* if(string.IsNullOrEmpty(Value)) {*
* return;*
* }*
* for(int i = 0; i < Value.Split(‘,’).Length; i++) {*
_ Points.Add(new Vector2(float.Parse(Value.Split(‘,’).Split(‘;’)[0].Replace(“(”,“”).Replace(“)”,“”).Split(‘:’)[0]),float.Parse(Value.Split(‘,’)[0].Split(‘;’)[0].Replace(“(”,“”).Replace(“)”,“”).Split(‘:’)[1])));
PointsName.Add(Value.Split(‘,’).Split(‘;’)[1]);
* PointsTransform.Add(((GameObject)Instantiate(OriginalPoint, new Vector3(0,0,0), Quaternion.identity)).transform);*_

_ worldCoordinate = projection(Points.ToArray());
pixelCoordinate = new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));
* pixelCoordinate = new Vector2(pixelCoordinate.x-playerPixelCoordinate.x, pixelCoordinate.y-playerPixelCoordinate.y);*_

PointsTransform.ToArray().position = new Vector3(pixelCoordinate.xPixelToMap, pixelCoordinate.yPixelToMap,-0.1f);
* }*
* }*
* }*

* // Use this for initialization*
* void Start () {*
* gm = gm.GetComponent();*
* asource = asource.GetComponent();*
* Input.location.Start(1f,1f);*
* Points = new List();*
* Load();*
* }*

* void OnApplicationQuit() {*
* Save();*
* }*

* // Update is called once per frame*
* void Update () {*
* if(Mathf.Abs(Input.acceleration.x)+Mathf.Abs(Input.acceleration.y)+Mathf.Abs(Input.acceleration.z)>=5.1f && asource.pitch>2.85f || Input.GetKeyDown(KeyCode.K)) {*
* asource.PlayOneShot(sucessSound);*
* StartCoroutine(ShowMessage("Congratulation! You found this point: " + PointsName.ToArray()[NearestPointIndex]));*
* }*

* if(LOADINTERVAL == 50) {*
* float ClosestDistance = Mathf.Infinity;*
* int index = 0;*

* float scale = 1 << gm.zoom;*
* Vector2 worldCoordinate; // = projection(new Vector2(0,0));*
_ Vector2 pixelCoordinate; //= new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));_

* Vector2 playerWorldCoordinate = projection(gm.LatLong);*
_ Vector2 playerPixelCoordinate = new Vector2(Mathf.Floor(playerWorldCoordinate.x * scale),Mathf.Floor(playerWorldCoordinate.y * scale));_

* for(int i = 0; i < Points.Count; i++) {*
_ worldCoordinate = projection(Points.ToArray());
pixelCoordinate = new Vector2(Mathf.Floor(worldCoordinate.x * scale),Mathf.Floor(worldCoordinate.y * scale));
* pixelCoordinate = new Vector2(pixelCoordinate.x-playerPixelCoordinate.x, pixelCoordinate.y-playerPixelCoordinate.y);*_

PointsTransform.ToArray().position = new Vector3(pixelCoordinate.xPixelToMap, pixelCoordinate.yPixelToMap,-0.1f);

_ if(Vector2.Distance(new Vector2(Input.location.lastData.longitude,Input.location.lastData.latitude), Points.ToArray()) < ClosestDistance) {
ClosestDistance = Vector2.Distance(new Vector2(Input.location.lastData.longitude,Input.location.lastData.latitude),Points.ToArray());
* index = i;
}
}
NearestPointIndex = index;
LOADINTERVAL = 0;
}
LOADINTERVAL++;*_

* if(SOUNDUPDATE == 20) {*
* if(Points.Count != 0) {*
* float Distance = Vector2.Distance(new Vector2(Input.location.lastData.longitude,Input.location.lastData.latitude), Points.ToArray()[NearestPointIndex]);*
* if(Distance < 0.00100f) {*
_ asource.pitch = (0.00100f-Distance)3000f;
} else {
asource.pitch = 0;
}
}
SOUNDUPDATE = 0;
}
SOUNDUPDATE++;
}*_

* Vector2 projection(Vector2 latLong) {*
_ float siny = Mathf.Sin(latLong.y * Mathf.PI / 180);_

* // Truncating to 0.9999 effectively limits latitude to 89.189. This is*
* // about a third of a tile past the edge of the world tile.*
* siny = Mathf.Min(Mathf.Max(siny, -0.9999f), 0.9999f);*

_ return new Vector2(gm.size * (0.5f + latLong.x / 360),gm.size * (0.5f - Mathf.Log((1 + siny) / (1 - siny)) / (4 * Mathf.PI)));
* }*_

* IEnumerator ShowMessage (string Message) {*
* if(messageTr.gameObject.activeInHierarchy == false) {*
* messageTr.gameObject.SetActive(true);*
* messageTr.GetChild(0).GetComponent().text = Message;*
* yield return new WaitForSeconds(2.8f);*
* messageTr.gameObject.SetActive(false);*
* }*
* yield break;*
* }*

}

On the device, there isn’t any file right after the build. So…

    if(File.Exists(Application.persistentDataPath+"/points.txt")) {
                 sw = File.CreateText(Application.persistentDataPath+"/points.txt");
             }

on Save() has no effect. Make sure you create the file first. Like this :

 if(!File.Exists(Application.persistentDataPath+"/points.txt")) {
             sw = File.CreateText(Application.persistentDataPath+"/points.txt");
         }