0

I am trying to use Xamarin.Auth for a Facebook signin. It's all set up, but I am missing the last part. The Facebook signin is not my MainActivity, one has to click on a button in order to sign in. I don't know how to start the page for the signin. I have trying to follow this approach, but as my MainActivity isn't the Facebook signin page, it won't work. I have binded (I am following the MVVM pattern) the signin button and have implemented an interface in order to use DependencyService. My problem is when I have to implement the code of the platforms.

This is what I have tried on Android:

class LoginFBImpl : Activity, ILoginFB, IFacebookAuthenticationDelegate
    {
        public void LoginFB() //the method in the interface
        {
            var auth = new FacebookAuthenticator(FacebookAuthenticator.ClientId, FacebookAuthenticator.Scope, this);
            var authenticator = auth.GetAuthenticator();
        var intent = authenticator.GetUI(this);
        StartActivity(intent); //Problem occurs here
        }

        public async void OnAuthenticationCompletedAsync(UserModel token)
        {
            var facebookService = new FacebookService();
            var name = await facebookService.GetNameAsync(token.AccessToken);
            var id = await facebookService.GetIdAsync(token.AccessToken);
            var picture = await facebookService.GetPictureAsync(token.AccessToken);
        }

        public void OnAuthenticationCancelled()
        {

        }

        public void OnAuthenticationFailed(string message, Exception exception)
        {

        }
    }

iOS:

public class LoginFBImpl : UIViewController, ILoginFB, IFacebookAuthenticationDelegate
    {
        public void LoginFB()
        {
            var auth = new FacebookAuthenticator(FacebookAuthenticator.ClientId, FacebookAuthenticator.Scope, this);
            var authenticator = auth.GetAuthenticator();
            var viewController = authenticator.GetUI();
            PresentViewController(viewController, true, null);
        }

        public async void OnAuthenticationCompletedAsync(UserModel token)
        {
            DismissViewController(true, null);

            var facebookService = new FacebookService();
            var name = await facebookService.GetNameAsync(token.AccessToken);
            var id = await facebookService.GetIdAsync(token.AccessToken);
            var picture = await facebookService.GetPictureAsync(token.AccessToken);
        }

        public void OnAuthenticationFailed(string message, Exception exception)
        {
            DismissViewController(true, null);

            var alertController = new UIAlertController
            {
                Title = message,
                Message = exception?.ToString()
            };
            PresentViewController(alertController, true, null);
        }

        public void OnAuthenticationCancelled()
        {
            DismissViewController(true, null);

            var alertController = new UIAlertController
            {
                Title = "Authentication cancelled",
                Message = "You didn't complete the authentication process"
            };
            PresentViewController(alertController, true, null);
        }
    }

I think it has something to do with the Activity/ViewController, but I don't know how to do it properly. When I run this I get: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference on Android, and I am expecting something similar on iOS - Haven't tested it yet as I am working on Windows.

user4476151
  • 39
  • 1
  • 6
  • I have a feeling you havn't googled enough before giving up. The main approach with Xamarin.Forms is to create a "fake" login content page that you are missing to start the auth process, and put all your code in its custom renderer. – Nick Kovalsky Feb 05 '18 at 17:47
  • @NickKovalsky: Have also tried that (http://www.joesauve.com/using-xamarin-auth-with-xamarin-forms/). However, it keeps telling me that the parameter app_id is required. I have set it. Created the project in Facebook Developer and got the app id from there. I navigate to https://www.facebook.com/v2.12/dialog/oauth?. Have also tried with "https://www.facebook.com/dialog/oauth/", but then it tells me that a problem occured, and it will be fixed soon. – user4476151 Feb 05 '18 at 19:26

0 Answers0