Bug 41859 - FormsApplicationDelegate does not expose window method
Summary: FormsApplicationDelegate does not expose window method
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 2.0.0
Hardware: Macintosh Other
: Normal enhancement
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-06-15 18:15 UTC by David Shaw
Modified: 2017-09-12 11:57 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description David Shaw 2016-06-15 18:15:45 UTC
When integrating a third party bound library into a Xamarin Forms app, I was getting a crash:

Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[AppDelegate window]: unrecognized selector sent to instance 0x124dcd030

This was caused by the following snippet of code in the library:

        UIWindow *mainWindow = [[UIApplication sharedApplication].delegate window];
        if(!mainWindow) {
            mainWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];

Because the AppDelegate for a forms application inherits from Xamarin.Forms.Platform.iOS.FormsApplicationDelegate and this "window" selector doesn't exist on that class, the app crashes when this Objective-C code runs in that library.

Apple's UIApplicationDelegate protocol reference is here:  


It states:

Providing a Window for Storyboarding
The window to use when presenting a storyboard.

optional var window: UIWindow? { get set }
@property(nonatomic, strong) UIWindow *window
This property contains the window used to present the app’s visual content on the device’s main screen.  Implementation of this property is required if your app’s Info.plist file contains the UIMainStoryboardFile key. 

While a Forms app does not need to implement this method because it doesn't use a storyboard, it should still respond to the selector 'window'.  I added a workaround in my app delegate that has the following:

        public UIWindow GetWindow()
            return UIApplication.SharedApplication.Windows[0];

This works fine and prevents the crash.  Something similar should be included in the base class, even if it simply returns null.
Comment 1 Jimmy [MSFT] 2017-03-07 18:29:25 UTC
Thank you for your feedback! As this has been classified as an enhancement request, it would be best suited as a UserVoice suggestion[1] or a proposal in our Evolution forum[2] for future consideration. When submitting a proposal on the Evolution forum, be sure to first read the contribution guidelines[3] so your proposal is ready for discussion with the Forms team and community.

[1] https://xamarin.uservoice.com/forums/144858-xamarin-platform-suggestions
[2] https://forums.xamarin.com/categories/xamarin-forms-evolution
[3] https://forums.xamarin.com/discussion/84503/please-read-first
Comment 2 mwesolowski 2017-09-12 11:57:30 UTC
We have also run into this issue. We have overridden UIWindow object that cannot be injected into FormsApplicationDelegate because it instantiate normal UIWindow. There is no way to access private FormsApplicationDelegate _window variable.

Proposed solution is to provide public Window property in FormsApplicationDelegate or use one already exposed in base UIApplicationDelegate and later check in FinishedLaunching method is Window is set then use. If window would be not set go with the current implementation and create new UIWindow.