Xamarin Essentials的FilePicker不允许在iOS上选取任何文件

kg7wmglp  于 2022-12-07  发布在  iOS
关注(0)|答案(1)|浏览(170)

我需要导入CSV/SQLite3文件到我的应用程序中,以恢复之前离线备份的设置。我使用Xamarin.Essentials.FilePicker,它在Android上运行良好,但在iOS上尝试时,所有文件都是灰色的,我无法选择任何文件。
我在Android和iOS中使用以下文件类型:

var customFileType =
    new Xamarin.Essentials.FilePickerFileType(new Dictionary<Xamarin.Essentials.DevicePlatform, IEnumerable<string>>
    {
        { Xamarin.Essentials.DevicePlatform.iOS, new string[] {
            "public.comma-separated-values-text",
            "text/csv",
            "comma-separated values",
            "CSV document",
            "csv",
            "public.database",
            "application/x-sqlite3",
            "application/vnd.sqlite3",
            "application/octet-stream",
        } },
        { Xamarin.Essentials.DevicePlatform.Android, new string[] {
            "text/csv",
            "Comma-separated values",
            "CSV document",
            "application/x-sqlite3",
            "application/vnd.sqlite3",
            "application/octet-stream"
        } },
    }) ;

我也尝试过在iOS上添加text/plain来选择CSV文件,但没有成功。另外,我也尝试过使用pdf来选择PDF文件,但也没有成功。
我觉得我遗漏了一些与我正在放置的MIME/UTType无关的东西,但到目前为止我找不到任何这样的东西。
任何帮助都将不胜感激。

bqf10yzr

bqf10yzr1#

this a weird issue that even I have faced and after scratching my head for a week I gave up(This was last year) I did not know this was still an issue in any case I would suggest you create a Bug in Essentials GitHub https://github.com/xamarin/Essentials/issues
And what you can do is for the time being use FilePicker Plugin(I am still using this): https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows
And then use it like this :

try
{
    FileData fileData = await CrossFilePicker.Current.PickFile();
    if (fileData == null)
        return; // user canceled file picking

    string fileName = fileData.FileName;
    string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);

    System.Console.WriteLine("File name chosen: " + fileName);
    System.Console.WriteLine("File data: " + contents);
}
catch (Exception ex)
{
    System.Console.WriteLine("Exception choosing file: " + ex.ToString());
}

This Library is marked deprecated by Gerald Versluis(Owner) but he will probably still accept PRs if needed in the future.
I hope this helps you out :)

相关问题