Maui: [BUG] Error using popup on iOS

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

I show a popup. When I close the popup, I have an exception

 An error occurred: 'Object reference not set to an instance of an object.'. Callstack: '   at CommunityToolkit.Maui.Core.Views.MauiPopup.SetShadowView(UIView& target)
   at CommunityToolkit.Maui.Core.Views.MauiPopup.ViewDidLayoutSubviews()
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at daze.dazeapp.Program.Main(String[] args) in C:\Daze\daze.app.maui\daze.dazeapp\daze.dazeapp\Platforms\iOS\Program.cs:line 12
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)'

I think the problem is that “target” sometimes is null

static void SetShadowView(in UIView target)
{
	if (target.Class.Name is "_UICutoutShadowView")
	{
		target.RemoveFromSuperview();
	}

	if (target.Class.Name is "_UIPopoverDimmingView")
	{
		target.BackgroundColor = UIColor.Black.ColorWithAlpha(0.4f);
	}

	foreach (var view in target.Subviews)
	{
		SetShadowView(view);
	}
}

maybe a check on “target” and “target.Subviews” should solve the problem…

Expected Behavior

It should not crash

Steps To Reproduce

I have a popup that I Use as loader… when I close the popup after some “works”, sometimes the exception is thrown

Link to public reproduction project repository

I haven’t one…

Environment

- .NET MAUI CommunityToolkit:8.0.1
- OS:iOS 17.3
- .NET MAUI: 8.0.21

Anything else?

No response

About this issue

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

Most upvoted comments

@MAUIoxo , I just created a PR, so please take a look at it.

I found the solution for this issue. I will create a PR after tomorrow. Just adding a few lines of code will solve problem.

@brminnick I am also facing this issue on the iPhone 11 or lower. The iOS version does not matter.

Steps to reproduce(1st approach):

  1. Open CommunityToolkit.Maui.Sample.sln in the samples folder.
  2. Choose the target device as iPhone 11 or lower
  3. Run the project
  4. Open Flyout window
  5. Choose “Views”
  6. Choose “Multi Popups Page” card
  7. Click “Simple Popup” button
  8. Once the Popup opens rotate the device, when you rotate the device for the third time the app will crash.

Steps to reproduce(2nd approach):

  1. Open CommunityToolkit.Maui.Sample.sln in the samples folder.
  2. Choose the target device as iPhone 11 or lower
  3. Run the project
  4. Rotate the device one time
  5. Open Flyout window
  6. Choose “Views”
  7. Choose “Multi Popups Page” card
  8. Click “Simple Popup” button
  9. The app will crash

The reason behind the crash is the PresentationController.ContainerView that is being passed in the SetShadowView method is null.

https://github.com/CommunityToolkit/Maui/blob/567eee2d1a1b2a3d3d6eba014b877027eaf31623/src/CommunityToolkit.Maui.Core/Views/Popup/MauiPopup.macios.cs#L55

1st approach Demo: https://github.com/CommunityToolkit/Maui/assets/5452331/2da060f3-143d-4ecc-8bfe-0961efce5509

2nd approach Demo: https://github.com/CommunityToolkit/Maui/assets/5452331/d6bbff50-656a-489f-a53f-f54357c0d9fa