Maui: [BUG] [iOS] Toast: AlertView not finding the KeyWindow

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Crashing on iOS device

System.InvalidOperationException: KeyWindow is not found
   at CommunityToolkit.Maui.Core.Views.AlertView.get_ParentView() in /_/src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs:line 17
   at CommunityToolkit.Maui.Core.Views.Alert..ctor() in /_/src/CommunityToolkit.Maui.Core/Views/Alert/Alert.macios.cs:line 17
   at CommunityToolkit.Maui.Core.Views.PlatformToast..ctor(String message, UIColor backgroundColor, CGRect cornerRadius, UIColor textColor, UIFont font, Double characterSpacing, NFloat padding) in /_/src/CommunityToolkit.Maui.Core/Views/Toast/PlatformToast.macios.cs:line 28
   at CommunityToolkit.Maui.Alerts.Toast.ShowPlatform(CancellationToken token) in /_/src/CommunityToolkit.Maui/Alerts/Toast/Toast.macios.cs:line 53
   at CommunityToolkit.Maui.Alerts.Toast.Show(CancellationToken token) in /_/src/CommunityToolkit.Maui/Alerts/Toast/Toast.shared.cs:line 77

Expected Behavior

Don’t crash.

Steps To Reproduce

using var t = Toast.Make("Something to say", CommunityToolkit.Maui.Core.ToastDuration.Long);
await t.Show();

Link to public reproduction project repository

(tbd)

Environment

- .NET MAUI CommunityToolkit: 1.3.0
- OS: iOS 15
- .NET MAUI: 6.0.536/6.0.400

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 19 (3 by maintainers)

Most upvoted comments

Just to help anyone else who might arrive here after doing the same thing as me - you can get this error if you try to make a Toast too early. I was trying to show Toast in a ContentView’s constructor (probably not a good place for it anyway), fixed by moving it inside a Loaded event callback.

The ASWebAuthenticationSession is 100% reproducible every time you press cancel, but I will need to get an example outside of my main app that shows this.