unable to access UIApplication.SharedApplication.KeyWindow during the launch of the app as keywindow

saisai Member ✭✭
edited January 23 in Xamarin.iOS

When my app is launched i show the Storyboad with an image in it like a splash screen. Then i call LoadApplication(new App()) from FinishedLaunching() method in AppDelegate class. In my App.cs, I need to access UIApplication.SharedApplication.KeyWindow.RootViewController to show a progress bar. But Keywindow is null and hence there is crash.

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
DependencyService.Register<ILoadingService,LoadingService>();
global::Xamarin.Forms.Forms.Init();
Xamarin.FormsGoogleMaps.Init("AIzaSyCqt-tfGrKhauCgC2Y5UkPreXfMZisPOH8");

LoadApplication(new App());
UIWindow.Appearance.TintColor = new UIColor(red: 0.55f, green: 0.76f, blue: 0.29f, alpha: 1.0f);
return base.FinishedLaunching(app, options);
}

App.cs file:

public App(string ticketNumberNotifParam = null, int ticketID = 0)
{
DependencyService.Get().Show("Updating user token...");
LoadHomepage();
}

LoadingService();

public void Show(string title, string message = "Loading")
{
UIViewController controller =
UIApplication.SharedApplication.KeyWindow.RootViewController;
hud = new MTMBProgressHUD(controller.View);
controller.View.AddSubview(hud);
}

Here Keywindow is null. Any suggestions what I'm doing wrong?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Try to use UIApplication.SharedApplication.Delegate.GetWindow() instead of UIApplication.SharedApplication.KeyWindow.

Sign In or Register to comment.