Search code examples
xamarin.iosfilepickerxamarin.essentials

Xamarin.Essentials Unable to select images in "File Picker" on iOS


When I try to select an image using the "File Picker" in Xamarin.Essentials, I can't select it only in the iOS simulator.

enter image description here

Xamarin.Essentials: File Picker
https://learn.microsoft.com/en-us/xamarin/essentials/file-picker?context=xamarin%2Fandroid&tabs=ios

var options = new PickOptions
{
  PickerTitle = "Please select files",
  FileTypes = FilePickerFileType.Images,
};

IEnumerable<FileResult> results = await FilePicker.PickMultipleAsync(options);

FilePickerFileType.Jpeg setting did not work for me. In addition, I could not select a PDF file with FilePickerFileType.Pdf settings.

However, when I launched the Android simulator, I was able to select the image successfully; only the iOS simulator was unable to select it.

Since PickerTitle is also not reflected, it seems that options itself is not enabled.

Also, even if I modified the customFileType area as pointed out in the answer below, it still did not allow me to select files.

var customFileType =
  new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
  {
      {DevicePlatform.iOS, new[] { "com.adobe.pdf" , "public.jpeg" } },
  });

var options = new PickOptions
  {
      PickerTitle = "Please select files",
      //FileTypes = FilePickerFileType.Images,
      FileTypes = customFileType,
  };

IEnumerable<FileResult> results = await FilePicker.PickMultipleAsync(options);

Do I need to set anything else?

  • Apple M1(macOS Monterey)
  • Visual Studio for Mac 8.10.17
  • Xamarin.Essentials 1.7.0
  • Xamarin.Forms 5.0.0.2291

Solution

  • Since your FileTypes property is set to FilePickerFileType.Images, pdf files are not allowed to select. So when setting FileTypes, you need to add it.

    Here is the background code:

    private void Button_Clicked(object sender, EventArgs e)
    {
        Task<FileResult> results =  PickAndShow();
    }
    async Task<FileResult> PickAndShow()
    {
        var customFileType =
            new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
            {
                {DevicePlatform.iOS, new[] { "com.adobe.pdf" , "public.image" } },
            });
        try
        {
            var result = await FilePicker.PickAsync(new PickOptions
            {
                PickerTitle = "Please select files",
                FileTypes = customFileType,  
            }) ;
            if (result != null)
            {
                Text = $"File Name: {result.FileName}";
                if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                    result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
                {
                    var stream = await result.OpenReadAsync();
                    Image = ImageSource.FromStream(() => stream);
                }
            }
            return result;
        }
        catch (Exception ex)
        {
            // The user canceled or something went wrong
        }
        return null;
    }
    

    The main difference is that I create a customFileType and use it when setting FileTypes.