Can't save textures on iOS (downloaded remotely)

Hello,

I’ve quickly made a simple cross-promotion system (based on showing more games images inside a UI) that reads a JSON file and grabs games images from Dropbox.

It’s divided into 2 parts:

  • manager: that handles the part of grabbing and saving data inside the mobile device
  • controller that is applied to a panel in the canvas that handles the button clicks and displaying the data on canvas.

it works as supposed in Android & in Editor

The problem I get is that it doesn’t work on iOS because of this error (a real error code saved on the Analytics tool):
“”"
UnauthorizedAccessException: Access to the path ‘/var/mobile/Containers/Data/Application/FA74D4C1-A48F-498A-8D8C-46EFF50D4636/Documents4-MoreGames7.png’ is denied. System.IO.FileStream…ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <00000000000000000000000000000000>:0) System.IO.File.Create (System.String path, System.Int32 bufferSize) (at <00000000000000000000000000000000>:0) System.IO.File.WriteAllBytes (System.String path, System.Byte[ ] bytes) (at <00000000000000000000000000000000>:0) MoreGManager+d__20.MoveNext () (at <00000000000000000000000000000000>:0) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <00000000000000000000000000000000>:0)
“”"

Apparently, even though we’re using Application.PersistentDataPath. the Script doesn’t save it on iOS devices.

Here is the part of the code doing the saving:

IEnumerator DownloadTextureData(GameData game)
    {
        string imgUrl = game.advertisingImageLink;
        string textureName = game.textureFullName;
        UnityWebRequest www = UnityWebRequestTexture.GetTexture(imgUrl);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Texture2D texture2D = ((DownloadHandlerTexture)www.downloadHandler).texture;
            byte[] bytes = texture2D.EncodeToPNG();
            string filePath = Application.persistentDataPath + textureName;
            if (!File.Exists(filePath))
            {
                // Save it only if it has never been saved before!
                File.WriteAllBytes(filePath, bytes);
                savedTextures.Add(1);
#if DEBUG_CrossPromPlugin
                Debug.Log("Texture Saved to: " + Application.persistentDataPath + textureName);
#endif
            }
            else
            {
                savedTextures.Add(1);
            }
        }
    }

As you can see, I’m using Application.persistentDataPath to save the texture. Does anyone have an idea why it doesn’t work on iOS?

Kind regards

Look at the path. Note how Documents is mooshed into your filename:

36/Documents4-MoreGames7.png

I suggest perhaps using System.IO.Path.Combine() instead of simple string concatenation.

Thanks for your suggestion. I will try this