question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to show OAuth Login from Xamarin.Forms modal Page

See original GitHub issue

Xamarin.Auth Issue

Version

  • component version = 1.7.0

Steps to reproduce

  1. Open a Xamarin.Forms modal page
  2. Create an OAuth2Authenticator
  3. Create an OAuthLoginPresenter
  4. Call OAuthLoginPresenter.Login from the modal page

I’ve attached a MCVE that reproduces the issue:

ModalAuthTest.zip

Platform:

  • .NET Standard 2.0
  • mono 6.6
  • iOS 13.1.2
  • Xamarin.iOS 13.8.1.1 (Preview)
  • Xcode 11.1
  • Visual Studio 16.4 (Preview 2)

(I am using the Visual Studio 16.4 P2 because debugging failed with 16.3, but the issue occurred when using 16.3 with Xamarin.iOS 13.2 and mono 6.0, too)

Expected behaviour

The application should show the OAuth login page to authenticate the user with the app.

Actual behaviour

The web view is not shown and a warning is displayed on the debug output

Warning: Attempt to present <SFSafariViewController: 0x10aa65600> on <Xamarin_Forms_Platform_iOS_PlatformRenderer: 0x10725f950> whose view is not in the window hierarchy!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
AlexSchuetzcommented, Mar 4, 2020

I experienced a similar problem, but with another warning. The problem originated in the Presenter choosing the wrong ViewController. Fortunately I was able to inject my own Presenter:

public class MyPresenter
{
        UIKit.UIViewController rootViewController;

        public void Login(Authenticator authenticator)
        {
            authenticator.Completed += AuthenticatorCompleted;

            rootViewController = GetCurrentUIController();
            rootViewController.PresentViewController(authenticator.GetUI(), true, null);
        }

        void AuthenticatorCompleted(object sender, AuthenticatorCompletedEventArgs e)
        {
            rootViewController?.DismissViewController(true, null);
            ((Authenticator)sender).Completed -= AuthenticatorCompleted;
            // always think of the GC ;)
            rootViewController = null;
        }

        private UIViewController GetCurrentUIController()
        {
            UIViewController viewController;
            var window = UIApplication.SharedApplication.KeyWindow;
            if (window == null)
            {
                throw new InvalidOperationException("There's no current active window");
            }

            viewController = window.RootViewController;

            while (viewController.PresentedViewController != null)
            {
                viewController = viewController.PresentedViewController;
            }

            return viewController;
        }
}

and in AppDelegate I changed:

//global::Xamarin.Auth.Presenters.XamarinIOS.AuthenticationConfiguration.Init();
global::Xamarin.Auth.Presenters.OAuthLoginPresenter.PlatformLogin = (authenticator) =>
{
        var oauthLogin = new MyPresenter();
        oauthLogin.Login(authenticator);
};

Currently I still have a error dialog popping up (cannot open URL), but authentication works. I will post an update as soon as I got rid of the error dialog.

UPDATE The “Cannot open URL”-error dialog was due to a configuration error (wrong URL scheme in info.plist).

0reactions
andygiklingcommented, Apr 28, 2021

Yes @AlexSchuetz thank you for the insight. I might have to step back and rethink this a bit here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to show OAuth Login from Xamarin.Forms modal Page
Open a Xamarin.Forms modal page · Create an OAuth2Authenticator · Create an OAuthLoginPresenter · Call OAuthLoginPresenter.Login from the modal page.
Read more >
ios - Unable to redirect to the authentication page using ...
Now I am using the OAuth2.0 for login functionality. So I have created one view controller in Xamarin iOS project. In the AppDelegate...
Read more >
Xamarin.Forms Modal Pages
Xamarin.Forms provides support for modal pages. A modal page encourages users to complete a self-contained task that cannot be navigated ...
Read more >
Build Login in Xamarin with Xamarin.Forms
Learn to build a cross-platform login form for iOS, Android, and Windows using Xamarin Forms.
Read more >
Implementing User Authentication with Sign in with Apple
In the sample app, LoginViewController displays a login form and a Sign in with Apple button ( ASAuthorizationAppleIDButton ) in its view hierarchy....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found