[英]Xamarin Firebase PhoneAuth for Xamarin Forms
我需要創建一個具有 Firebase 電話身份驗證的 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 Essentials,則可以僅使用Platform.CurrentActivity
來獲取 MainActivity 的實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.