如何在Xamarin.Forms(iOS和Android的共享项目)中使用Xamarin.Auth?

时间:2018-02-05 17:22:09

标签: xamarin xamarin.forms xamarin.ios xamarin.android xamarin.auth

我正在尝试使用Xamarin.Auth进行Facebook登录。这一切都已经完成,但我错过了最后一部分。 Facebook登录不是我的MainActivity,必须单击按钮才能登录。我不知道如何启动登录页面。我试图遵循this方法,但由于我的MainActivity不是Facebook登录页面,因此无法正常工作。我已经绑定(我遵循MVVM模式)登录按钮并已实现接口以使用DependencyService。我的问题是当我必须实现平台的代码时。

这是我在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);
        }
    }

我认为它与Activity / ViewController有关,但我不知道如何正确地做到这一点。当我运行这个时,我得到:java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference在Android上,我期待在iOS上有类似的东西 - 当我在Windows上工作时,Haven还没有对它进行测试。

0 个答案:

没有答案