Use UIAlertController in AppDelegate

SteJoSteJo NLMember
edited November 2017 in Xamarin.iOS

Hi There, i hope someone can help me, I try to replace the deprecated alerts in an app my colleague has build in C#.
Unfortunately I can not find much about my problem

The app now show PushNotifications in an Alert.
However i like to remove all the warnings and want to use UIAlertController to handle the alerts.

In the whole app this code works:

var pushView = UIAlertController.Create("Message title", "Message content", UIAlertControllerStyle.Alert);
pushView.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => Console.WriteLine("Push message, ok button was clicked")));
PresentViewController(pushView, true, null);

However in the AppDelegate the PresentViewController is not accessable directly.
So I find that I should use

UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(pushView, true, null);

But this returns a System.NullReferenceExeption

So I find an other solution to view the Alert.

Window = new UIWindow(UIScreen.MainScreen.Bounds);
Window.RootViewController = new UIViewController();
Window.MakeKeyAndVisible();
Window.RootViewController.PresentViewController(pushView, true, null);

Now it creates a empty window with the Alert.
Because Window overrides the current Window variable.
Can someone tell me how I can show the Alert in my current view

See here the most of my AppDelegate.cs

 using Foundation;
 using UIKit;
 using HockeyApp.iOS;
 using System;

 namespace App-nameIOS
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to application events from iOS.
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
    // class-level declarations
    public static new AppDelegate Self { get; private set; }
    public override UIWindow Window
    {
        get;
        set;
    }

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        // Override point for customisation after application launch.
        // If not required for your application you can safely delete this method
        AppDelegate.Self = this;
        RegisterForNotifications();
        var manager = BITHockeyManager.SharedHockeyManager;
        manager.Configure("**********");
        manager.StartManager();
        manager.Authenticator.AuthenticateInstallation();

        // ##################################################################
        //
        //  I added a function TestMessage to simulate the push notification part. 
        //  I skipped the push notification part since that was working correctly
        //  only viewing the messing went wrong but the TestMessage simulates that.
        //
        // ##################################################################

        TestMessage();
        return true;
    }

    public void TestMessage()
    {
        var pushView = UIAlertController.Create("Message title", "Message content", UIAlertControllerStyle.Alert);
        pushView.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => Console.WriteLine("Push message, ok button was clicked")));
        //UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(pushView, true, null);

        Window = new UIWindow(UIScreen.MainScreen.Bounds);
        Window.RootViewController = new UIViewController();
        Window.MakeKeyAndVisible();
        Window.RootViewController.PresentViewController(pushView, true, null);
    }
}
}

Best Answer

  • SteJoSteJo NL
    edited November 2017 Accepted Answer

    After trial and error multiple times the working solution seems quite easy.

     var pushView = UIAlertController.Create("You have a new message", inAppMessage.ToString(), UIAlertControllerStyle.Alert);
     pushView.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => Console.WriteLine("Push message, ok button was clicked")));
     Window.MakeKeyAndVisible();
     this.Window.RootViewController.PresentViewController(pushView, true, null);
    

Answers

  • TedRogersTedRogers USMember ✭✭✭✭

    Creating a new Window would be a bad idea. Why not use the existing Window that your AppDelegate should have?

  • SteJoSteJo NLMember
    edited November 2017 Accepted Answer

    After trial and error multiple times the working solution seems quite easy.

     var pushView = UIAlertController.Create("You have a new message", inAppMessage.ToString(), UIAlertControllerStyle.Alert);
     pushView.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => Console.WriteLine("Push message, ok button was clicked")));
     Window.MakeKeyAndVisible();
     this.Window.RootViewController.PresentViewController(pushView, true, null);
    
Sign In or Register to comment.