Search code examples
xamarinxamarin.formsxamarin.iosfilesystemsxamarin.essentials

How does Xamarin.Essentials deal with the iOS “Documents” folder?


I followed the “build mobile apps with Xamarin.Forms” course. On the “store local data with SQLite in a Xamarin.Forms app” section, they say iOS provides two folders to deal with the files saved on a device:

  • the Documents folder, for user-generated data only.
  • the Library folder, for for app-generated data.

Then, this subsection is presented:

enter image description here

What I don’t understand is: is this the correct way to deal with the iOS Documents folder? I ask this because they explicitly say libFolder contains the appropriate Library location. What about the Documents folder required by Apple’s guidelines?


Solution

  • There are detailed information about different purposes of folders in iOS, in this document:

    Documents:/

    Use this directory to store user documents and application data files.

    The contents of this directory can be made available to the user through iTunes file sharing (although this is disabled by default). Add a UIFileSharingEnabled Boolean key to the Info.plist file to allow users to access these files.

    Even if an application doesn’t immediately enable file sharing, you should avoid placing files that should be hidden from your users in this directory (such as database files, unless you intend to share them). As long as sensitive files remain hidden, these files will not be exposed (and potentially moved, modified, or deleted by iTunes) if file sharing is enabled in a future version.

    You can use the Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments) method to get the path to the Documents directory for your application.

    The contents of this directory are backed up by iTunes.

    Library/:

    The Library directory is a good place to store files that are not created directly by the user, such as databases or other application-generated files. The contents of this directory are never exposed to the user via iTunes.

    You can create your own subdirectories in Library; however, there are already some system-created directories here that you should be aware of, including Preferences and Caches.

    The contents of this directory (except for the Caches subdirectory) are backed up by iTunes. Custom directories that you create in Library will be backed up.

    Please read the document and feel free to ask if you have other problems.

    Update

    to access update answer:

    iOS:

    string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    string libFolder = System.IO.Path.Combine(docFolder, "..", "Library");
    

    Android:

    string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);