IOS File Path for saving data

Hi All

So been stuck for a while on this so any help is appreciated.
Basically, the following code works perfectly in the editor to save currency data but on iOS builds, the data does not save. I think it has something to do with the save file path on iOS not being right because im not getting any errors. If someone could see where its going wrong in my code, it would be greatly appreciated as this problem is halting my release.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

class LevelSave
{

private byte[] IV = { 0x6c, 0x1e, 0x85, 0x5e, 0x97, 0x4a, 0x9e, 0x39, 0x9b, 0x80, 0x33, 0x31, 0x5d, 0x76, 0x6e, 0xc5 };
private byte[] Key = { 0x78, 0x06, 0x2f, 0x16, 0x3a, 0x5f, 0x4d, 0xcc, 0xe4, 0x28, 0xb7, 0x6f, 0x75, 0x1b, 0xd4, 0xb8 };

void Awake (){
	Environment.SetEnvironmentVariable ("MONO_REFLECTION_SERIALIZER", "yes");
}

public void save(string toFile, ref List<LevelData> levels)
{

    FileStream fileStream = new FileStream(toFile, FileMode.Create);
    RijndaelManaged RMCrypto = new RijndaelManaged();

    CryptoStream CryptStream = new CryptoStream(fileStream, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
    BinaryWriter writer = new BinaryWriter(CryptStream);

    for (int i = 0; i < levels.Count; i++)
    {

        writer.Write(levels*.locked);*

}
writer.Close();
CryptStream.Close();
fileStream.Close();
}
public void load(string fromFile, ref List levels)
{
if (!File.Exists(fromFile))
return;
try
{
FileStream fileStream = new FileStream(fromFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(fileStream, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
BinaryReader reader = new BinaryReader(CryptStream);
for (int i = 0; i < levels.Count; i++)
{
levels*.locked = reader.ReadBoolean();*
}
reader.Close();
CryptStream.Close();
fileStream.Close();
}
catch (Exception e)
{
return;
}
}
}

These are just examples to show you what you could do as I pulled them from my own code and tried to make them geared towards what you need. Because if you’re deploying this on multiple platforms you will need to check which platform you are on to create said directory correctly.

        private static string GetPathBasedOnOS()
        {
            if (Application.isEditor)
                return "file://" + Application.persistentDataPath + "/";
             else if (Application.isWebPlayer)
                return System.IO.Path.GetDirectoryName(Application.absoluteURL).Replace("\\", "/") + "/";
            else if (Application.isMobilePlatform || Application.isConsolePlatform)
                return Application.persistentDataPath;
            else // For standalone player.
                return "file://" +  Application.persistentDataPath + "/";
        }

Could also make a function that creates the directory. Something like this:

static public string CreateDirectory()
        {
            // Choose the output path according to the build target.
            string outputPath = Path.Combine(GetPathBasedOnOS(), "YourGameNameSaveDir");
            if (!Directory.Exists(outputPath))
                Directory.CreateDirectory(outputPath);

            return outputPath;
        }

You could also make this better by declaring your game name in a string variable and use that instead of the quotes with your game name in it.

Like I said, I altered this code to fit more of your situation. The original code works for me, you might have to tinker to make this work for you. :smiley: