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

ArgumentNullException on UIApplication.SupportedInterfaceOrientationsForWindow #22278

Closed
MichaelLHerman opened this issue Mar 3, 2025 · 3 comments · Fixed by #22285
Closed
Labels
bug If an issue is a bug or a pull request a bug fix
Milestone

Comments

@MichaelLHerman
Copy link

Apple platform

iOS

Framework version

net9.0-*

Affected platform version

MAUI 9.0.21

Description

Getting a crash report with the following stack trace

System.ArgumentNullException: ArgumentNull_Generic Arg_ParamName_Name, window
  ?, in NativeHandle NativeObjectExtensions.GetNonNullHandle(INativeObject, string)
  ?, in UIInterfaceOrientationMask UIApplication.SupportedInterfaceOrientationsForWindow(UIWindow)
  ?, in UIInterfaceOrientationMask AppDelegate.GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
  ?, in UIInterfaceOrientationMask __Registrar_Callbacks__.callback_11_CadabraMobile_iOS_AppDelegate_GetSupportedInterfaceOrientations(IntPtr pobj, IntPtr sel, IntPtr p0, IntPtr p1, IntPtr* exception_gchandle)

Apple marks the window parameter as nullable, but the generated code seems to be doing a null check.

Steps to Reproduce

My app is overriding supportedInterfaceOrientationsForWindow because we disallow rotation on all but one screen, so this can not be hardcoded in the info.plist

        [Export("application:supportedInterfaceOrientationsForWindow:")]
        public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application,
            UIWindow? forWindow)
        {
            if (forWindow?.WindowScene != null && AllowRotation)
            {
                return UIInterfaceOrientationMask.All;
            }

            return application.SupportedInterfaceOrientationsForWindow(forWindow);
        }

Crash is being reported for iPad Pro (12.9-inch) (3rd generation) running iOS 17.6, app in foreground

Did you find any workaround?

No response

Relevant logs

No response

@rolfbjarne
Copy link
Member

According to Apple's documentation, the window parameter to SupportedInterfaceOrientationForWindow can't be null: https://developer.apple.com/documentation/uikit/uiapplication/supportedinterfaceorientations(for:)?language=objc, so this looks correct.

[Export("application:supportedInterfaceOrientationsForWindow:")]
public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application,
    UIWindow? forWindow)
{
    if (forWindow?.WindowScene != null && AllowRotation)
    {
        return UIInterfaceOrientationMask.All;
    }

	if (window is null)
		return UIInterfaceOrientationMask.Portrait; // or whatever is desired for your app when there's no window

    return application.SupportedInterfaceOrientationsForWindow(forWindow);
}

@rolfbjarne rolfbjarne added the need-info Waiting for more information before the bug can be investigated label Mar 4, 2025
@rolfbjarne rolfbjarne added this to the Future milestone Mar 4, 2025
@MichaelLHerman
Copy link
Author

MichaelLHerman commented Mar 4, 2025

@rolfbjarne I'm no iOS expert but I dont think the objective-c documentation has nullability info

See https://developer.apple.com/documentation/uikit/uiapplicationdelegate/application(_:didfinishlaunchingwithoptions:) as another example of a method with a nullable input parameter

launchOptions parameter can be null as shown in the Swift documentation but theres no indication in the Objective-C version of the page (that the pointer can be null).

See https://developer.apple.com/documentation/uikit/uiapplication/supportedinterfaceorientations(for:) (no query string, Swift documentation) that marks window parameter as nullable

The stack trace shows that clearly on some device under some conditions the method is being called with a null window

The xamarin wrapper for GetSupportedInterfaceOrientations is generated with window as not nullable and I assume that means its injecting NativeObjectExtensions.GetNonNullHandle which is throwing

@dotnet-policy-service dotnet-policy-service bot added need-attention An issue requires our attention/response and removed need-info Waiting for more information before the bug can be investigated labels Mar 4, 2025
@rolfbjarne
Copy link
Member

OK, I see. I've prepared a fix, and this should be fixed in the next minor release.

@rolfbjarne rolfbjarne added bug If an issue is a bug or a pull request a bug fix and removed need-attention An issue requires our attention/response labels Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug If an issue is a bug or a pull request a bug fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants