UnauthorizedAccessException: Access to the path is denies Ios

12,925

You save with Application.persistentDataPath not Application.dataPath. Note that you must create a folder and save inside that folder instead of saving directly to this path.

So the final path you save to should be:

Application.persistentDataPath+"Yourfolder/YourFileNale.extension".

or

Application.persistentDataPath+"Documents/YourFileNale.extension".

Always use Path.Combine to Combine paths instead of "+" I used above.

Share:
12,925
Kaushik Ray
Author by

Kaushik Ray

Updated on June 05, 2022

Comments

  • Kaushik Ray
    Kaushik Ray almost 2 years

    I know this has come up some time but I am not able to solve the error with any of the solutions I tried.

    I just started testing my application - to save a screenshot to ios device Code is -

    string GetiPhoneDocumentsPath()
    {
        string path = Application.dataPath.Substring(0, Application.dataPath.Length - 5);
        path = path.Substring(0, path.LastIndexOf("/"));
        return path + "/Documents/";
    }
    
    string CreateImagesDirectory(string documentsPath) {
        //System.IO.File.SetAttributes (documentsPath, FileAttributes.Normal);
        string imagePath = documentsPath +"magicimages/";
        if (Directory.Exists(imagePath)) {return imagePath;}
        DirectoryInfo t = new DirectoryInfo(documentsPath);
        //Directory.CreateDirectory(imagePath);
        t.CreateSubdirectory("magicimages");
        System.IO.File.SetAttributes (imagePath, FileAttributes.Normal);
    
        return imagePath;
    }
    

    To wite the file

        Debug.Log("Do nothing actually as we need to save to persistent data path");
            string documentsPathIphone = GetiPhoneDocumentsPath();
            Debug.Log ("document path" + documentsPathIphone);
            string imagePath = CreateImagesDirectory (documentsPathIphone);
            //Path = imagePath + fileName;
            Debug.Log ("path iphone" + Path);
            Debug.Log("Appllicarion data path -->" + Application.dataPath);
            //string savepath = Application.dataPath.Replace ("game.app/Data", "/Documents/");
            System.IO.File.WriteAllBytes (System.IO.Path.Combine(Path , fileName), screenshot);
    

    Assuming screenshot is bytes[]

    I get the exception that as in subject. I am using Unity.

    Error that I get is -

    UnauthorizedAccessException: Access to the path "/var/containers/Bundle/Application/9DA2D489-2037-451E-87D1-FA7354ECD0D1/Documents" is denied.

  • Kaushik Ray
    Kaushik Ray almost 7 years
    Before I can create the directory I get the error in CreateImagesDirectory() so even the folder creation is not happening
  • Programmer
    Programmer almost 7 years
    What do you expect me to do if you get an error and don't post it? ............ if (!Directory.Exists(Path.GetDirectoryName(yourPath))) { Directory.CreateDirectory(Path.GetDirectoryName(yourPath)); }...... then ....... File.WriteAllBytes(yourPath, yourData);
  • Kaushik Ray
    Kaushik Ray almost 7 years
    ok, I updated the methods with the code and error that I am getting. the exists option is available in CreateImagesDirectory method. Can you please check now?
  • Programmer
    Programmer almost 7 years
    You also need to add Edit and add the new code ans also mention the line of coed that's causing that error.
  • Kaushik Ray
    Kaushik Ray almost 7 years
    I solved it actually now. The issue was with datapath vs persistentdata path. using persistent one resolved the issue. Thanks anyways
  • Programmer
    Programmer almost 7 years
    This is what I said in my answer. I said you should use Application.persistentDataPath not Application.dataPath. I don't know what you have been using since then.