On tvOS, Application.persistentDataPath is empty and I cannot save anything to disk

Hi, I have a problem: I cannot save files to the persistent folder when building for tvOS. It works in the Unity Editor and when I build an Mac app. But not when I build for tvOS. I have tested with Unity version 2019.2.13 and 2019.2.15 and I get the same problem. I test my app by hooking up the AppleTV with a USB and running it through Xcode.

The first thing I noticed is that Application.persistentDataPath is Empty (only on tvOS). After some googling I found out that some people use Environment.GetFolderPath(Environment.SpecialFolder.Personal) instead. Using that path I get a folder path:
/private/var/mobile/Containers/Data/Application/791C94D2-CB3A-46DA-9E14-025EFA5633FE
But when I try to write anything to that folder I get an exception:
System.UnauthorizedAccessException: Access to the path … is denied.

I found out that some people say you canno create anything in the root of that folder. So I tested to create a folder in my Personal folder. But I get the same UnauthorizedAccessException exception.

So the next thing I tested was to create a folder inside the Documents folder in my Personal folder. But I got the same UnauthorizedAccessException.

I can see the folder inside the Personal folder using Directory.GetDirectories(), but I cannot write anything.

Can anyone please help? I have been struggling with this for so long now.

Step to replicate:

  1. Create a new blank unity project.
  2. Set target to tvOS.
  3. Add this script to a gameobject:
     Debug.Log("Persistant: " + Application.persistentDataPath);
        Debug.Log("MyDocuments: " + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
        Debug.Log("Personal: " + Environment.GetFolderPath(Environment.SpecialFolder.Personal));

        Debug.Log( "Is persistant null? " + (Application.persistentDataPath == null) );
        Debug.Log( "Is persistant empty? " + (Application.persistentDataPath == string.Empty) );

        string path = "";
        path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
       
        if( !path.EndsWith("Documents") )
        {
            path = Path.Combine(path, "Documents");
        }
       
        string dirpath = Path.Combine(path, "testdir");
       
        try
        {
            Directory.CreateDirectory(dirpath);
        }
        catch (Exception er)
        {
            Debug.Log("Failed to create directory: " + dirpath);
            Debug.LogError(er);
        }

This is the output I get:

Persistant:
MyDocuments: /private/var/mobile/Containers/Data/Application/791C94D2-CB3A-46DA-9E14-025EFA5633FE

Personal: /private/var/mobile/Containers/Data/Application/791C94D2-CB3A-46DA-9E14-025EFA5633FE

Is persistant null? False

Is persistant empty? True

Failed to create directory: /private/var/mobile/Containers/Data/Application/791C94D2-CB3A-46DA-9E14-025EFA5633FE/Documents/testdir

System.UnauthorizedAccessException: Access to the path "/private/var/mobile/Containers/Data/Application/791C94D2-CB3A-46DA-9E14-025EFA5633FE/Documents/testdir" is denied.
  at System.IO.Directory.CreateDirectoriesInternal (System.String path) [0x00000] in <00000000000000000000000000000000>:0
  at Tester.Start () [0x00000] in <00000000000000000000000000000000>:0

I don’t own an Apple tv or build for one, but that was a quick Google search.

Thank you. I have been googling for a whole day and somehow I have missed that you can’t store files on the appletv. Thanks again.

Just a note for anyone else wanting to write data on AppleTV: you can write files to the /tmp folder during execution. But that folder can be cleared by the OS between runs.

1 Like