Xamarin Firebase PhoneAuth

时间:2018-03-20 21:19:23

标签: firebase xamarin firebase-authentication

我需要创建一个具有Firebase Phone Auth的Xamarin应用程序(不是表单,但是分开的iOS和Android项目)。大多数文档表明支持Facebook Auth,但我也需要PhoneAuth。

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

1 个答案:

答案 0 :(得分:3)

Android和iOS都需要在Firebase控制台和平台特定的应用程序设置中进行设置,因此 Google Firebase Auth文档是必读的,用户需要通知每个应用的电话使用情况/费用各个国家的分销法律要求(在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