0

我正在尝试使用 Xamarin.Auth 进行 Facebook 登录。一切都准备好了,但我错过了最后一部分。Facebook 登录不是我的 MainActivity,必须单击一个按钮才能登录。我不知道如何启动登录页面。我试图遵循这种方法,但由于我的 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 上工作时还没有测试它。

4

0 回答 0