【问题标题】:Xamarin Firebase PhoneAuthXamarin Firebase PhoneAuth
【发布时间】:2018-03-20 21:19:22
【问题描述】:

我需要创建一个具有 Firebase 电话身份验证的 Xamarin 应用(不是表单,而是分离的 iOS 和 Android 项目)。大多数文档表明支持 Facebook Auth,但我也需要 PhoneAuth。

我找到了 Xamarin.Firebase.Auth,显然它支持它,但我无法实现它,也没有看到任何关于我应该使用哪个接口的文档。

【问题讨论】:

    标签: firebase xamarin firebase-authentication


    【解决方案1】:

    Android 和 iOS 都需要在 Firebase 控制台和平台特定的应用程序设置中进行设置,因此Google Firebase 身份验证文档是必读的,并且用户需要根据您的应用程序了解手机使用情况/费用各个国家/地区的分发法律要求(在 Firebase 文档中注明...),iOS 需要 APN 通知设置等...

    Android 通过Xamarin.Firebase.Auth 示例:

    文档:https://firebase.google.com/docs/auth/android/phone-auth

    PhoneAuthCallbacks phoneAuthCallbacks = new PhoneAuthCallbacks();
    PhoneAuthProvider.Instance.VerifyPhoneNumber("555-555-5555", 60, TimeUnit.Seconds, this, phoneAuthCallbacks);
    // You can now obtain a user credential via the verification code and verification ID and 
    

    // 通过凭据登录用户(参见 OnVerificationStateChangedCallbacks)

    PhoneAuthProvider.OnVerificationStateChangedCallbacks 类示例:

    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.         
        }
    
        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.
        }
    
        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);
        }
    }
    

    iOS 通过Xamarin.Firebase.iOS.Auth 示例:

    文档:https://firebase.google.com/docs/auth/ios/phone-auth

    var verificationID = await PhoneAuthProvider.DefaultInstance.VerifyPhoneNumberAsync("555-555-5555", null);
    // You can now obtain a user credential via the verification code and verification ID.
    // Now you can sign the user in via the credential
    

    【讨论】:

    • 能否请您告诉我们如何访问回调中的活动(我们将其作为第三个参数传递给 VerifyPhoneNumber)?
    • 别忘了把+1放在"555-555-5555之前,否则它不会工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2019-08-01
    • 2018-11-26
    • 1970-01-01
    • 2021-08-02
    • 2017-09-18
    相关资源
    最近更新 更多