Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FilePicker on MacCatalyst app is not working (occasionally) #15126

Open
petervaradi opened this issue May 17, 2023 · 21 comments
Open

FilePicker on MacCatalyst app is not working (occasionally) #15126

petervaradi opened this issue May 17, 2023 · 21 comments
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/macOS 🍏 macOS / Mac Catalyst s/needs-attention Issue has more information and needs another look s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@petervaradi
Copy link

Description

FilePicker returns null as soon as it pops up, 8 out of 10 times.
var result = await FilePicker.Default.PickAsync();

Steps to Reproduce

  1. Create a new .Net MAUI App on Mac in Visual Studio.
  2. In the MainPage.xaml.cs add the following (to the sample button handler OnCounterClicked:
var result = await FilePicker.Default.PickAsync();
if (result != null)
{
    int u = 0;  
}
  1. Put a breakpoint on the "if" statement and run the app.
  2. Click on the button on the sample app.

This should let you choose a file and the name should return in the result above.
But instead it sometime just returns with the result null as soon as the dialog pops up.

If you don't see the bug, try to restart the app, or rebuild it.

Link to public reproduction project repository

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

macOS

Affected platform versions

macOS Ventura 13.3.1 x64

Did you find any workaround?

No response

Relevant log output

No response

@petervaradi petervaradi added the t/bug Something isn't working label May 17, 2023
@Eilon Eilon added the area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info label May 17, 2023
@PureWeen PureWeen added platform/macOS 🍏 macOS / Mac Catalyst s/needs-repro Attach a solution or code which reproduces the issue labels May 17, 2023
@ghost
Copy link

ghost commented May 17, 2023

Hi @petervaradi. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@petervaradi
Copy link
Author

There is a duplicate bug I just found:
#11088

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-repro Attach a solution or code which reproduces the issue labels May 17, 2023
@LukeFranky
Copy link

Similar thing happening on iOS 16.1.1 using FilePicker.PickMultipleAsync when debugging a remote device, but the enumerable is empty instead of null.

Due to its inconsistent nature, I cannot fully confirm it only happens in debug mode, but so far it seems to be the case.

@brightertools
Copy link

I am having this issue on MacOs using PickMultipleAsync

It works in dev but does not work after deploying via TestFlight, its works on PC deployed via windows store.

This component does pretty much one thing, how is taking so long to fix? (considering the original issue #11088)

I have tried the dependency injection and direct calls, is there any definitive fix or work around? or is this a capabilities settings?

CommunityToolkit.Maui 5.2.0
Apple M2 Max, Ventura 13.4.1

@petervaradi
Copy link
Author

I ended up using this as a workaround:
https://github.com/datvm/LukeMauiFilePicker

@brightertools
Copy link

brightertools commented Jul 31, 2023

I ended up using this as a workaround: https://github.com/datvm/LukeMauiFilePicker

Thanks for the link, however I tried this but gives me the filename not the full path to the file and its no longer supported as per the issue that also mentions this problem. Did you have this issue?

UPDATE:

I forked that repro and fixed it [tested via TestFlight], forked version here:

https://github.com/brightertools/MauiFilePicker

@XamlTest XamlTest added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Nov 21, 2023
@XamlTest
Copy link

Verified this on Visual Studio Enterprise 17.9.0 Preview 1(8.0.3). Repro on MacCatalyst, not repro on Windows 11, Android 14.0-API34 and iOS 17.0 with below Project:
15126.zip

@scottkuhl
Copy link

I ran into this as well. MacOS does not work with the FilePicker.

@Khongchai
Copy link

Any official update on this? This is blocking our progress rn. Picking a file is one of the first things users have to do when using our app :\

@jfversluis
Copy link
Member

@datvm I see you have a fix in your plugin and mentioned maybe doing a PR, I guess you never got to that? Would you be able to point out what the difference is between your code and ours and give a hint where this might originate?

@datvm
Copy link

datvm commented Feb 21, 2024

@jfversluis hi, isn't there a fix (and explanation of the problem) posted in #11088 already (this comment)? Is this a different one? It's been a while but IIRC my library simply rewrites the logic for file picker for iOS and Mac Catalyst from scratch in this file. I didn't rely on MAUI code at the time so I am not really sure the difference.

Also an update that I am back to MAUI development in the recent months, time to revisit this issue I think.

@jfversluis
Copy link
Member

@datvm this was merged which I think implements that comment effectively, but I just tested it earlier with the latest bits and it still doesn't seem to work. If you have indeed some insights on the current status and a potential fix if necessary, I would be very interested!

@rmarinho
Copy link
Member

rmarinho commented Apr 7, 2024

Yeah just hitted this too.

@NykuluzM
Copy link

NykuluzM commented Apr 9, 2024

Omg this is one of my main functionality of my app 😂 and my only way to develop is thru a macbook can anyone suggest for workaround?

@Khongchai
Copy link

Khongchai commented Apr 9, 2024

@NykuluzM

I made this small gist and it did the trick for me.

using UIKit;
using UniformTypeIdentifiers;

#nullable enable

namespace MyServices
{
  public partial class MacFilePicker
  {
        public static async Task<FileResult?> PickAsync(PickOptions options)
        {
            var tcs = new TaskCompletionSource<string>();

            var utTypes = options.FileTypes?.Value.Select(q =>
                UTType.CreateFromIdentifier(q) ??
                UTType.CreateFromMimeType(q) ??
                UTType.CreateFromExtension(q) ??
                UTTypes.Item)
            .ToArray();

            var picker = new UIDocumentPickerViewController(
            utTypes ?? Array.Empty<UTType>(),
            asCopy: true)
            {
                AllowsMultipleSelection = false
            };

            TaskCompletionSource<FileResult?> filesTcs = new();
            picker.DidPickDocumentAtUrls += (_, e) =>
            {
                if (e.Urls.Length == 0)
                {
                    filesTcs.TrySetResult(null);
                    return;
                }
                var path = e.Urls[0].Path;
                if (path == null)
                {
                    filesTcs.TrySetResult(null);
                    return;
                }
                filesTcs.TrySetResult(new FileResult(path));
            };

            picker.WasCancelled += (_, e) =>
            {
                filesTcs.TrySetResult(null);
            };

            var controller = Platform.GetCurrentUIViewController();
            ArgumentNullException.ThrowIfNull(controller);

            await controller.PresentViewControllerAsync(picker, true);

            return await filesTcs.Task;
        }
  }
}

You can use it like this

#if MACCATALYST
                result = await MacFilePicker.PickAsync(pickOptions);
#else
                result = await FilePicker.PickAsync(pickOptions);
#endif

Not even sure whether this is the best way to do this but it works.

I think I modified it from the original MAUI code or something, I don't remember.

@datvm
Copy link

datvm commented Apr 10, 2024

@Khongchai interesting, it looks similar to my code at https://github.com/datvm/LukeMauiFilePicker/blob/master/LukeMauiFilePicker/FilePickerService.MaciOS.cs (but then I probably copied it from MAUI code as well). Are you sure it is working well for Mac? I can copy the differences to my library code then.

@NykuluzM
Copy link

I fixed my problem using @datvm 's nugget thank you so much <3 i will also try your solution @Khongchai it seems to have unique functionalities too

@NykuluzM
Copy link

eee.mov

Working 🎉🎉

@Khongchai
Copy link

@datvm Well @NykuluzM seems to already have his issue fixed using your solution. So I think there's no need to copy mine...who knows my code might even be based on yours, haha. It's been a while.

@PanWuming
Copy link

Hi everyone,

I've been working on this issue for the past few days and have some insights to share.

It appears that the issue with the FilePicker occasionally not working on MacCatalyst is related to the frameworks being used in the MAUI source code. Specifically, I noticed that MAUI is using AppKit, whereas the implementation that successfully returns file selection information is based on UIKit.

Here's a brief explanation:

MAUI Source Code Usage: The current MAUI implementation relies on AppKit for file picking operations.
UIKit Implementation: On the other hand, the working implementation of file selection that returns the correct information seems to be based on UIKit.
This mismatch between AppKit and UIKit might be causing the intermittent issues with the FilePicker functionality on MacCatalyst.

I believe aligning the MAUI implementation with UIKit, or ensuring proper interoperability between AppKit and UIKit, could potentially resolve this issue.

Looking forward to hearing thoughts from others and the maintainers on this.

Thank you!

@MartyIX
Copy link
Contributor

MartyIX commented Dec 20, 2024

MAUI Source Code Usage: The current MAUI implementation relies on AppKit for file picking operations.
UIKit Implementation: On the other hand, the working implementation of file selection that returns the correct information seems to be based on UIKit.
This mismatch between AppKit and UIKit might be causing the intermittent issues with the FilePicker functionality on MacCatalyst.

Just a heads up, this PR #26743 removes a file that was not in use. I think it might have caused some confusion that it references AppKit. It's my feeble attempt to make it easier to fix this issue1.

If there is a bug on Mac Catalyst, then this file should be checked out: https://github.com/dotnet/maui/blob/main/src/Essentials/src/FilePicker/FilePicker.ios.cs

Footnotes

  1. I didn't try to repro this issue so I'm not really sure if it's still an issue or not. I don't question its existence, I just didn't reproduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/macOS 🍏 macOS / Mac Catalyst s/needs-attention Issue has more information and needs another look s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests