When you make an Xamarin forms app, the iOS target app's AppDelegate inherits from a base class as follows:
public class AppDelegate : Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
The problem I am having is that we have some native code in a library that does the following:
UIWindow *mainWindow = [[UIApplication sharedApplication].delegate window];
In Xamarin this results in an error message:
Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[AppDelegate window]: unrecognized selector sent to instance 0x124dcd030
Is there a reason that the base class doesn't have access to the 'window' selector, and could this be remedied, either in my own AppDelegate
subclass or in the Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
base class?
Posts
Ok I have a workaround that seems to work for now. I added this to my own AppDelegate subclass:
This seems to work. However it still seems like a bug that the window property of AppDelegate is not available in a forms app. Should I submit this as something to be fixed?
Hi, David. I voted this is also a workaround for me.
I found this error after updated Xamarin Studio in early of July 2016. The error occurred with Google Mobile Ads for iOS component (7.8.1).
The code you suggest fixed it.
I don't sure the reason that raise the bug. I rolled back to use Google Mobile Ads for iOS 7.7.1.2 doesn't solved. But it happened after I updated the Xamarin Studio.
@DavidShaw THanks for the work around.
@DavidShaw Thanks for posting this workaround.
Hi @TeerasejJiraphatchandej,
I have the same problem with Ads. Could you fix it please?
Thank you in advance.
Hey guys,
I found this explanation for Android and iOS and it's working fine.
http://puresourcecode.com/dotnet/post/Xamarin-Forms-and-Google-Mobile-Ads-for-iOS
I had the same issue with the PayPal Here SDK wrapper. This workaround seems to fix it. Thanks for sharing it.
Glad I could help Enrico. Please give credit to where you found the workaround.
Is it guaranteed that the first window [0] will always be the one created by the FormsApplicationDelegate?
Does anyone of you tried to report exposing FormsApplicationDelegate Window feature on platforms suggestion portal?
https://xamarin.uservoice.com/forums/144858-xamarin-platform-suggestions