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

[BUG] FileSaver on iOS crashing when started from a modal #1138

Closed
2 tasks done
softlion opened this issue Apr 13, 2023 · 1 comment · Fixed by #1142
Closed
2 tasks done

[BUG] FileSaver on iOS crashing when started from a modal #1138

softlion opened this issue Apr 13, 2023 · 1 comment · Fixed by #1142
Labels
bug Something isn't working unverified

Comments

@softlion
Copy link
Contributor

softlion commented Apr 13, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

Crash log:

Attempt to present <UIDocumentPickerViewController: 0x1252d06a0> on 
<Microsoft_Maui_Controls_Platform_Compatibility_ShellFlyoutRenderer: 0x10a9aa160> (from 
<Microsoft_Maui_Controls_Platform_Compatibility_ShellFlyoutRenderer: 0x10a9aa160>) which is already presenting 
<Microsoft_Maui_Controls_Platform_ModalWrapper: 0x139806360>.

Expected Behavior

Don't crash.

Or at least give a way to choose the UIViewController on which to display the documentPickerViewController.

Steps To Reproduce

<ContentPage ...
             Shell.NavBarIsVisible="False"
             Shell.PresentationMode="ModalAnimated"
             xmlns:iOsSpecific="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
             iOsSpecific:Page.UseSafeArea="True"
             iOsSpecific:Page.ModalPresentationStyle="Automatic"
             >
    <VerticalStackLayout>
          <Button Command="{Binding PickCommand}" />
    </VerticalStackLayout>
</ContentPage>

Code in PickCommand:

            await using var stream = File.OpenRead(finalExportFilePath);
            var fileSaverResult = await FileSaver.SaveAsync(exportTitle, stream, CancellationToken.None);

Link to public reproduction project repository

Needs reproduction

Environment

- .NET MAUI CommunityToolkit: 5.0.0
- OS: iOS
- .NET MAUI: 7.0.81 / 7.0.100 / SDK 7.0.200

Anything else?

The source code is using this key window to display the controller:

		var currentViewController = Microsoft.Maui.Platform.UIApplicationExtensions.GetKeyWindow(UIApplication.SharedApplication)?.RootViewController;
		currentViewController?.PresentViewController(documentPickerViewController, true, null);

Example corner cases that changes the key window hierarchy.

  • screen recording is ON.
  • screen sharing is ON. For ex using the microsoft store app "AirPlay Screen Mirroring Receiver"
  • a "popup" is already presented (this case)

Instead, you should use:

Platform.GetCurrentUIViewController()
 var presentingVc = Platform.GetCurrentUIViewController();
if (presentingVc != null)
{
    UIApplication.SharedApplication.InvokeOnMainThread(() =>
    {
        var yourVC = ...
        presentingVc.PresentViewController(yourVC, true, null);
    });
}
else
    Log?.LogWarning("Input: no window/nav controller on which to display");
@softlion softlion added bug Something isn't working unverified labels Apr 13, 2023
@softlion
Copy link
Contributor Author

softlion commented Apr 13, 2023

Also there is another bug in the implementation.

If currentViewController is null, PresentViewController is not called, and the taskCompetedSource is never completed. Thus freezing the app.

To fix it, you could add:

if (currentViewController == null)
	throw new ("No UIViewController from which to display the document picker");

VladislavAntonyuk added a commit that referenced this issue May 2, 2023
* fix #138

* Is not null, fix disposable

---------

Co-authored-by: Vladislav Antonyuk <33021114+VladislavAntonyuk@users.noreply.github.com>
Co-authored-by: Vladislav Antonyuk <vlad.antonyuk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unverified
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant