UnauthorizedAccessException: Access to the path [ iOS / Unity3D ]

10,823

Solution 1

alright...

today i have the chance to test a fix that i come up with, thanks to Needleski.

and yes, it work, for some reason, iOS dont allow me to save a file (in this case any file) to Application.persistentDataPath directly, instead, what i do is, i create a Folder, then create/save the file inside the folder, and it work correctly now

Solution 2

Looking at the exception and comparing it to your code, I don't think the code your provided is where the error is occurring... for 2 reasons:

  1. The exception says "UnauthorizedAccessException: Access to the path"/var/mobile/Containers/Data/Application/280DC265-ABD8-41A3-8B87-74D09910FB24/Documentstemp.gjk" is denied". However, in your code you have /vendorBillboard/ in your path. Notice /vendorBillboard/ is not in the path in the exception.

  2. At the end of the exception it states the problem is in System.IO.File.CreateText (System.String path), however, you have used System.IO.File.WriteAllBytes() in your code.

I'm guessing you are creating a text file "somewhere else" and you are using Application.dataPath there instead of Application.persistentDataPath.

Share:
10,823
Solid_Metal
Author by

Solid_Metal

normal human being

Updated on June 05, 2022

Comments

  • Solid_Metal
    Solid_Metal almost 2 years

    i need your help, so yesterday i want to port my game to iOS, this is not my first time porting it, the previous version always port sucessfully, but not now

    i'm using Unity 5.3.4 and 5.3.5 with Xcode 7.3 on El Capitan, its sucessfully compile from the XCode, but all the object that i initilize and spawn at the start of the scene wont show up on the game, i dont know whats wrong with it since theres no error on Unity or XCode itself

    but from the logs, i suspect its because of this

    UnauthorizedAccessException: Access to the path"/var/mobile/Containers/Data/Application/280DC265-ABD8-41A3-8B87-74D09910FB24/Documentstemp.gjk" is denied. at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0 at System.IO.StreamWriter..ctor (System.String path, Boolean append) [0x00000] in :0 at System.IO.File.CreateText (System.String path) [0x00000] in :0

    its i assume because, when launch, the game will access texture/sprite from secure website using WWW method, with HTTP not HTTPS, and try to download and save it as sprite at Application.PersistentDataPath and then apply it into the game

    its working fine on Android but somehow iOS wont allow to read/write the file, hench the error

    heres snippet of the code i use :

    IEnumerator getBillboard()
    {
        WWW getBillboardlistURL = new WWW(billboardlistURL);
        yield return getBillboardlistURL;
    
        if (getBillboardlistURL.error != null)
        {
    
        }
        else
        {
    
    
            WWW imageLink = new WWW(pictLink[Mathf.FloorToInt(i / 3)]);
            yield return imageLink;
            Texture2D imageTemp = imageLink.texture;
            obj.transform.GetChild(0).GetComponent<SpriteRenderer>().sprite = Sprite.Create(imageTemp, new Rect(0, 0, imageTemp.width, imageTemp.height), new Vector2(0.5f, 0.5f));
            obj.name = namaProduk[Mathf.FloorToInt(i / 3)];
    
                    print("BILLBOARD ONLINE");
    
            /// ======= WRITE BILLBOARD NAME TO FILE AND SAVE IT =======
    
    
    
            /// ======================= END ============================
    
    
            var bytes = imageLink.texture.EncodeToPNG();
    
            //print(Application.dataPath);
    
            //cek file
            if(redownloadImage)
                    {
                        File.WriteAllBytes(Application.persistentDataPath + "/vendorBillboard/" + namaProduk[Mathf.FloorToInt(i / 3)] + ".png", bytes);
                        print("REDOWNLOAD SEMUA TEXTURE!");
                    }
    
                    obj.SetActive(false);
                    billboardPool.Add(obj);
                }
            }
    
        }
    }
    

    is there workaround or fix for this?

    thank you in advance