适用于Xamarin Forms的Xamarin Firebase PhoneAuth

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了适用于Xamarin Forms的Xamarin Firebase PhoneAuth相关的知识,希望对你有一定的参考价值。

我需要创建一个具有Firebase Phone Auth的Xamarin Forms应用程序。有一个Xamarin Firebase身份验证包,带有电话验证,但没有Xamarin Forms的文档。

到目前为止,对于android,我有:

public class FirebaseAuthenticator : IFirebaseAuthenticator
    {
        public async Task<string> RegisterWithPhoneNumber(string number)
        {
            PhoneAuthCallbacks phoneAuthCallbacks = new PhoneAuthCallbacks();
            var user = await PhoneAuthProvider.Instance.VerifyPhoneNumber(number, 60, TimeUnit.Seconds, this, phoneAuthCallbacks);
            return user;
        }
    }

我的PhoneAuthCallbacks类:

public class PhoneAuthCallbacks : PhoneAuthProvider.OnVerificationStateChangedCallbacks
    {
        public override void OnVerificationCompleted(PhoneAuthCredential credential)
        {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verification without
            //     user action.        
            FirebaseAuth.Instance.SignInWithCredential(credential);
            System.Diagnostics.Debug.WriteLine("onVerificationCompleted");
        }

        public override void OnVerificationFailed(FirebaseException exception)
        {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            System.Diagnostics.Debug.WriteLine("onVerificationFailed: " + exception);
        }

        public override void OnCodeSent(string verificationId, PhoneAuthProvider.ForceResendingToken forceResendingToken)
        {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.
            base.OnCodeSent(verificationId, forceResendingToken);
            System.Diagnostics.Debug.WriteLine("onCodeSent" + verificationId);
        }
    }

但它不起作用。在RegisterWithPhoneNumber中,'this'给出错误“无法从xx.Droid.FirebaseAuthenticator转换为Android.App.Activity”

答案

由于您的FirebaseAuthenticator不是Activity中固有的,因此您无法在调用函数中使用它。您可以将Main Activity保存为静态变量,并在依赖服务中使用。添加了以下代码

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    internal static Activity CurrentActivityRef { get; private set; }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        var fapp = FirebaseApp.InitializeApp(this);

        LoadApplication(new App());
        CurrentActivityRef = this;

    }

}

并在您的依赖服务实现中

public class FirebaseAuthenticator: IFirebaseAuthenticator
{

    public void RegisterOrLoginWithMobileNumber(string MobileNumber)
    {

        Activity activity = MainActivity.CurrentActivityRef;
        PhoneAuthCredentialCallBack callBack = new PhoneAuthCredentialCallBack();
        PhoneAuthProvider.Instance.VerifyPhoneNumber(MobileNumber,
                                                                  60,
                                                                  TimeUnit.Seconds,
                                                                  activity, callBack
                                                                  );

    }
}

以上是关于适用于Xamarin Forms的Xamarin Firebase PhoneAuth的主要内容,如果未能解决你的问题,请参考以下文章

适用于 UWP 的 Xamarin Forms 顶部导航栏

Xamarin.Forms 中编辑器的边框颜色

将按钮添加到标题栏 Xamarin Forms

Xamarin Forms AppShell 如何返回上一页

Xamarin.Forms 相机控制访问 DependencyService

适用于 iOS 的 Xamarin 表单:启动画面显示蓝色边框