Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Editor & General Support' started by gregzo, Aug 12, 2014.

  1. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    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:

    Code (CSharp):
    1. using System.IO;
    2.  
    3. public class StreamingAssetsTest : Monobehaviour
    4. {
    5.      public string relativePath;
    6.  
    7.      void Start()
    8.      {
    9.           string fullPath = Path.combine( Application.streamingAssetsPath, relativePath );
    10.           if( File.Exists( fullPath )
    11.           {
    12.                using( FileStream fs = new FileStream( fullPath, FileMode.Open ) ) // Nopey. Access denied...
    13.                {
    14.                      
    15.                }
    16.           }
    17.      }
    18. }
    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
     
    luma2057 likes this.
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    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.
     
  3. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    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.
     
  4. F.Salka

    F.Salka

    Joined:
    Dec 13, 2013
    Posts:
    43
    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
     
  5. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    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
     
  6. plingativator

    plingativator

    Joined:
    May 2, 2013
    Posts:
    13
    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!
     
    KibsgaardUnityStudios likes this.
  7. Al95

    Al95

    Joined:
    Nov 8, 2016
    Posts:
    6
    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

    Code (CSharp):
    1. using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
    2. {
    3.  
    4. }
     
    amokto likes this.
  8. MR_Jxin2

    MR_Jxin2

    Joined:
    Feb 24, 2023
    Posts:
    1
    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.