简体   繁体   中英

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?

在此输入图像描述

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM