2

I created app, which Authenticate using Azure AD

In Android it is working fine, but in iOS, it need RootViewController for load the page. But UIApplication.SharedApplication.KeyWindow is null. So I am not able to get UIApplication.SharedApplication.KeyWindow.RootViewController

Bellow is the code:

var authResult = await authContext.AcquireTokenAsync(
    graphResourceUri, 
    ApplicationID, 
    new Uri(returnUri), 
    new PlatformParameters(UIApplication.SharedApplication.KeyWindow.RootViewController)
);

Any other way from which I can get RootViewController

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
SSSA Group
  • 21
  • 1
  • 3
  • Can you show where in your application you are calling await authContext.AcquireTokenAsync? Maybe you are calling it to early? – ChristiaanV Aug 03 '17 at 06:51
  • try this : UIApplication.SharedApplication.Delegate.GetWindow().RootViewController; – ColeX Aug 03 '17 at 07:04
  • @ColeXia Wouldn't change anything if it is called too early. – tequila slammer Aug 03 '17 at 07:13
  • Yeah,if you call this "AcquireTokenAsync" function before the window generates , the return of rootViewcontroller will be null certainly. – ColeX Aug 03 '17 at 07:17
  • Your KeyWindow is "null" unless your view did appear. So override "ViewDidAppear()" and you can access "KeyWindow" and of course its "RootViewController". – M. Hartner Mar 28 '18 at 10:37

4 Answers4

3

This looks stupid but works.

        UIWindow window = UIApplication.SharedApplication.KeyWindow;
        UIViewController presentedVC = window.RootViewController;
        while (presentedVC.PresentedViewController != null)
        {
            presentedVC = presentedVC.PresentedViewController;
        }
Bright Lee
  • 2,306
  • 2
  • 27
  • 55
0

I tried this code also, but it is not working.

I got the root cause of this issue. Issue was, when I am going to access the RootViewController then there should be at least one page Initialized, but it is not Initialized so I am unable to get RootViewController

so I gave daily to Initialized the page then I got the RootViewController

SSSA Group
  • 21
  • 1
  • 3
0

Access RootViewController after your window is actually created. Do this after base.FinishedLauching, like this:

var result = base.FinishedLaunching(app, options);
var platformParameters = UIApplication.SharedApplication.KeyWindow.RootViewController;
App.AuthenticationClient.PlatformParameters = new PlatformParameters(platformParameters);
return result;
frido
  • 13,065
  • 5
  • 42
  • 56
0

Depending on the type of window you have loaded getting the RootViewController can be problematic. This version has been the most stable one I've tried thus far and avoids tail recursive loops.

    public UIViewController GetRootController(){
        var root = UIApplication.SharedApplication.KeyWindow.RootViewController;
        while (true)
        {
            switch (root)
            {
                case UINavigationController navigationController:
                    root = navigationController.VisibleViewController;
                    continue;
                case UITabBarController uiTabBarController:
                    root = uiTabBarController.SelectedViewController;
                    continue;
            }

            if (root.PresentedViewController == null) return root;
            root = root.PresentedViewController; 
        }
    }