Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - UnAuthorized Access Exception

This is a question I am sure Xcode developers could also answer. I have a screenshot of my code below in Xamarin.

Why am I getting an Unauthorized access exception? I should be able to write to the documents folder right?

enter image description here

Code:

  var webClient = new WebClient();
        //var documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // iOS 7 and earlier
        var documentsFolder = NSBundle.MainBundle.BundlePath;
        var fileNameAndPath = Path.Combine (documentsFolder, "verses.xml");


        if(!File.Exists(documentsFolder)){

            Directory.CreateDirectory(documentsFolder);

            if(!File.Exists(fileNameAndPath)){

                //File.SetAttributes(fileNameAndPath, FileAttributes.Normal);
                File.Create(fileNameAndPath);
                //Throws exception here.

            }

        }

And ERRORs:

Access to the path "/var/mobile/Applications/1F95D694-BBA5-4FB3-AE6C-0C2CDD9DEDD8/comexample.app/verses.xml" is denied

Access to the path '/private/var/mobile/Applications/1F95D694-BBA5-4FB3-AE6C-0C2CDD9DEDD8/Documents/verses.xml' is denied.

I have tried both paths and I get access denied.

like image 983
james0200101 Avatar asked Jun 11 '26 09:06

james0200101


1 Answers

You can't write to your app's bundle path, the folder is readonly. I'm also not sure where the Personal folder points to with Xamarin.iOS.

Use this directory, and you can write to it:

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
like image 51
jonathanpeppers Avatar answered Jun 13 '26 21:06

jonathanpeppers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!