xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.52k stars 505 forks source link

[Bug] FilePicker does not work on iOS #1710

Open vg12345 opened 3 years ago

vg12345 commented 3 years ago

Reproduced on iOS v14.4 Xamarin.Essentials v1.6.1 Xamarin.Forms v5.0.0.2012

  1. Call for await FilePicker.PickAsync()
  2. After browser window is open tap on any file
  3. Window is not closed after picking the file (tap), PickAsync does not return

Code example:

private async void OnPickButtonClicked(object sender, EventArgs e) { var file = await FilePicker.PickAsync(); }

Attached is the sample project App6.zip

dimonovdd commented 3 years ago

I tested your sample. It works perfectly. Could you provide more information? I also added a display of the resulting file to your sample: App6.zip

vg12345 commented 3 years ago

The problem is that picker (browsing) window is not closing automatically when I tap on the file. I just tap on the file and nothing happens. I use real device (iPhone 7 with iOS 14.4) and try to pick file from "On My iPhone" location. BTW, if I use the deprecated NuGet package Xamarin.Plugin.FilePicker, it works perfectly.

dimonovdd commented 3 years ago

@vg12345 Do you have this issue only with iPhone 7 (iOS 14.4)?

vg12345 commented 3 years ago

This is the only iOS device I work with

vg12345 commented 3 years ago

I tried to pick TXT, PNG and ZIP files Picker

dimonovdd commented 3 years ago

@vg12345 I tested this on a real iphone and simulator and everything works correctly.

Perhaps your problem is related to this one? https://github.com/xamarin/Essentials/issues/1587 https://github.com/xamarin/Essentials/issues/1612

vg12345 commented 3 years ago

I'm not sure. I have Mono interpreter enabled. The picker still does not work on iPhone that is disconnected from PC after deploy.

dimonovdd commented 3 years ago

I would really like to help you, this feature is very important to me too. But I can't reproduce this bug.

vg12345 commented 3 years ago

Hi,

Finally, I debugged Essentials FilePicker code on my device. The problem is in the following row that does not work for me (FilePicker.ios.cs, line 27):

using var documentPicker = new UIDocumentPickerViewController(allowedUtis, UIDocumentPickerMode.Open);

If I change the Open flag to Import, then everything works as expected:

using var documentPicker = new UIDocumentPickerViewController(allowedUtis, UIDocumentPickerMode.Import);

gmarbury commented 3 years ago

All constructors using UIDocumentPickerMode are obsolete as of IOS 14.0

dimonovdd commented 3 years ago

https://github.com/xamarin/Essentials/issues/1402

jamesmontemagno commented 3 years ago

@dimonovdd should we change the picker mode in the short term?

jamesmontemagno commented 3 years ago

https://docs.microsoft.com/en-us/xamarin/ios/platform/document-picker#open-vs-import-mode-and-the-document-picker this is tricky

dimonovdd commented 3 years ago

should we change the picker mode in the short term?

That's a good question. I use the FilePicker in several of my apps. I haven't any problems with it on iOS. Perhaps the import mode will be more flexible, but how will it work with large files?

jamesmontemagno commented 3 years ago

The other question is: Is this issue only with Hot Restart? Does it work fine without it? May just be a limitation.

dimonovdd commented 3 years ago

Perhaps your problem is related to this one?

1587

1612

@vg12345 said his problem is not related to #1587

vg12345 commented 3 years ago

May be it’s better to add the open mode as an optional parameter, so the developers will be allowed to decide themselves in which mode the want to open it?

jamesmontemagno commented 3 years ago

Add a gitignore file to your root directly and then sync it with github.

https://github.com/xamarin/Essentials/blob/main/.gitignore

dimonovdd commented 3 years ago

Add a gitignore file to your root directly and then sync it with github.

@jamesmontemagno you must have mixed up the issue. #1723

flimtix commented 3 years ago

I have the same problem on all physical iOS devices. ⚠️ But on Android, everything works fine. Are there any solutions?

vg12345 commented 3 years ago

You can use the code from Plugin.FilePicker for iOS – it works fine. It is relatively small effort to integrate it in your app.

GitHub - jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows: FilePicker Plugin for Xamarin and Windowshttps://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows

EvoPulseGaming commented 3 years ago

It is crazy that after all this time, this is still an issue. Also look at my branch: https://github.com/EvoPulseGaming/FilePicker-Plugin-for-Xamarin-and-Windows I use this for android/ios/uwp. I have not looked at anything added to xamarin in over a year so they probably have most of this, but this may help you figure something out.

mpfj commented 3 years ago

Not working for me either !!

I even tried the "AppEssentials" demo app and even that doesn't work for me. This is on an iPhone 8 (iOS 14.7.1)

I tried recording the app but you can't see any of the touch gestures so it's a bit pointless.

LITTOMA commented 3 years ago

Same issue for me...

cdavidyoung commented 2 years ago

Same issue for me using iPhone 12 pro. The only way to return from the picker is to cancel, which returns a null.

In my case I am exporting a .txt file to a local folder with Share.RequestAsync and then turning around and trying to import it with FilePicker.PickAsync(). What could be simpler?

cdavidyoung commented 2 years ago

The problem is evidently on the Windows side only. I just tried the same project on VSMac and the picker selected the file successfully. To be clear, the problem is happening when I am debugging from my Windows machine running VS 2022 and the iPhone directly attached via USB.

cdavidyoung commented 2 years ago

So I just started testing on an iPad Pro and I immediately hooked it up to my Windows laptop because it is so much easier to provision with the Wildcard on the VS Windows side. In testing the Share.RequestAsync I only wasted a couple of hours this time before running across my post from a couple of months ago! Yes, the bug still exists on the VS Windows side but the Share.RequestAsync works fine on the VS Mac side.

alextorres50 commented 2 years ago

it still happens, it's weird because sometimes it works fine, other times it disables, I tried on VS Mac and doesn't work

jamesmontemagno commented 2 years ago

I have recently integrated the FilePicker into one of my apps and it is working fine when built and deployed via testflight.

I don't believe the picker works on simulators and also could have sandbox issues via hot restart as well. Best approach is to test via testflight or plug in a device into your mac for full testing.

flimtix commented 2 years ago

@jamesmontemagno Did you build the app using a Mac? If so, try it through a Windows computer and without Testflight.

I am asking this because I have heard from some that this error does not appear when building directly on a Mac.

alextorres50 commented 2 years ago

@jamesmontemagno Did you build the app using a Mac? If so, try it through a Windows computer and without Testflight.

I am asking this because I have heard from some that this error does not appear when building directly on a Mac.

I build the app using a Mac, with settings M1 Chip , MacOs BigSur 11.6.5, Visual Studio Mac 2019 Enterprise (8.10.20), Xamarin.IOs 15.6.0.3 with the last version of XF (5.0.0.2401), and Essentials (1.7.1) and doesn't work

alextorres50 commented 2 years ago

I used a Console.WriteLine() to show exceptions and the log show me:

(DocumentManager)[2597] : Failed to create a url from bookmarkableString (Error Domain=NSCocoaErrorDomain Code=257) (DocumentManager)[2597] : tried to call delegate -documentBrowser:didPickDocumentURLs: with an empty array of items. This indicates the items failed to be prepared and materialized on disk:

this appear after clicking on the file to select it.

jamesmontemagno commented 2 years ago

Are you running it on an emulator or a physical device? It only works for me on a physical device.

This is my direct code:

async Task Import()
        {
            try
            {
                var options = new PickOptions
                {
                    PickerTitle = "Please My Cadence Data File",
                };

                var result = await FilePicker.PickAsync(options);
                if (result == null)
                    return;

                if(!result.FileName.EndsWith("txt", StringComparison.OrdinalIgnoreCase) || !result.FileName.StartsWith("mycadence"))
                {
                    await Application.Current.MainPage.DisplayAlert("Invalid file", "Please select the data file that starts with 'mycadence' and has an extension of '.txt'.", "OK");
                    return;
                }

                IsBusy = true;

                var json = File.ReadAllText(result.FullPath);

                var data = JsonConvert.DeserializeObject<ImportExportFile>(json);

                if(data?.Version != "1")
                {
                    await Application.Current.MainPage.DisplayAlert("Invalid data", "This version of My Cadence data is not supported on this version of the app.", "OK");
                    return;
                }

                if ((data?.Rides?.Count ?? 0) <= 0)
                {
                    await Application.Current.MainPage.DisplayAlert("Invalid data", "No rides were found in the selected data.", "OK");
                    return;
                }

                /*var choices = new[] { "Merge with existing data", "Override existing data" };
                var import = await Application.Current.MainPage.DisplayActionSheet($"Located {data.Rides.Count} rides, how would you like to import the data?", "Cancel", null, choices);
                if (import == "Cancel")
                    return;*/

                var confirm = await Application.Current.MainPage.DisplayAlert("Confirm import", $"Located {data.Rides.Count} rides, are you sure you want to import these rides? They will be merged into existing data.", "Import data", "Cancel");
                if (!confirm)
                    return;

                RideHistoryPage.ImportedData = true;

                //will add all in
                var addedCount = await database.AddAllCheckForDuplicates(data.Rides);

                await Application.Current.MainPage.DisplayAlert("Import complete", $"{addedCount} unique new rides have been merged into your existing data.", "OK");

            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Unable to import data", $"The following error occured, please email support: {ex}", "OK");
            }
            finally
            {
                IsBusy = false;
            }
        }
alextorres50 commented 2 years ago

Greetings to all,

I have identified the error, it seems to be due to the provisioning profiles.

Indeed, in the simulator this stopped working, but I also had this error in physical devices, using kobiton and applivery to load the app, they did not allow selecting files, as well as using a physical device, what I did was use automatic provisioning by linking my account from apple and using the VS WildCard Development profile and the selector is no longer frozen when I click on the document to select.

@jamesmontemagno Is it something we should worry about? since I have always used manual provisioning and it had not failed...

It should be clarified that the provisioning profiles and certificates are not expired

BlueRaja commented 2 years ago

It's been almost a year and a half, and something as simple as opening a file is still not working. Incredible.

[Edit] Seems to only be an issue with Hot Restart. When compiling using an actual mac, the dialog works correctly.

FreakyAli commented 2 years ago

It's been 2 years man, At least do something about it!!!!!!! @jfversluis Bro your old package works with this https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows can you please unarchive it at least this is Frustrating to another degree!!!!!!!

LITTOMA commented 2 years ago

It's been 2 years man, At least do something about it!!!!!!! @jfversluis Bro your old package works with this https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows can you please unarchive it at least this is Frustrating to another degree!!!!!!!

Tried Uno.Platform last week and I feel comfortable now. Their file pickers works smoothly even in WebAssembly!

Yes, my app runs just in the browser with the awesome WinUI design which is invented by Microsoft but not fully compatible by Microsoft.MAUI!

datvm commented 1 year ago

I am having an issue where the Picker shows up but no file can be selected (all files are grayed out). Is it the same issue?

UPDATE: apparently it's an iOS simulator bug. I tried writing platform specific code and the same problem happens. No problem running on real device though.