Accessing files in StreamingAssets on iOS - Access Denied???

Hi,

I’m getting an access denied exception when opening a file stream on a file placed in StreamingAssets.
Everything’s hunky dory in the editor and in desktop builds, but on iOS, nopey. The path seems correct, the file exists, but no access…

Example code:

using System.IO;

public class StreamingAssetsTest : Monobehaviour
{
     public string relativePath;

     void Start()
     {
          string fullPath = Path.combine( Application.streamingAssetsPath, relativePath );
          if( File.Exists( fullPath )
          {
               using( FileStream fs = new FileStream( fullPath, FileMode.Open ) ) // Nopey. Access denied...
               {
                     
               }
          }
     }
}

I found other users posting about similar issues in 2013, with no solution in sight.

Can this be? Surely, I’m missing something here…

Many thanks in advance for your help,

Gregzo

1 Like

Found this in the docs:

Which seems to apply to Android and web player, but who knows?

One solution would be to include files I need access to in the Xcode bundle and call [ NSBundle pathForResource… ] to retrieve the path in managed code. But that very much breaks the multi-platform workflow Unity is great for.

Bumping, no one having a similar issue? Full error message:

UnauthorizedAccessException: Access to the path “/var/mobile/Applications/098B9E5F-BFFF-47AA-BC5F-D12AA8CE9CD2/Test.app/Data/Raw/File.ext” is denied.

iOS 7.1, Unity 4.5.3, Xcode 5.

Your problem is that you need to use File.OpenRead, FileMode.Open has read/write capabilities and that is prohibited by iOS unless you are using a jailbroken device, use the following:

myStream = File.OpenRead(myPath); //Read only access…

Tell me if that helps

9 Likes

Yes to you!

I was chatting about the issue with one of my users who wasn’t experiencing it, never occurred to me he could have a jailbroken device…

Many thanks for the help,

Gregzo

I’m super late to this party, but I wanted to add that I was getting a similar issue running the game on some mac/windows machines, and the OpenRead() was what seems to have fixed it. Really glad I found this thread!

1 Like

I had a similar problem recently and I managed to read file on iOS from StreamingAssets using File.Open and specifying the FileAccess to Read like this

using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
{

}
1 Like

This method is useful, using a read-only method, the problem that has troubled me for a day has been solved, thank you for your solution.