티스토리 뷰

728x90
반응형

Xamarin.Android에 이어서..

using System;
using System.Threading.Tasks;
using ######..iOS.Services;
using ######.Models.FirebasePageModel;
using ######.Services.Account;
using Firebase.Auth;
using Foundation;
using Xamarin.Forms;

[assembly: Dependency(typeof(AccountService))]
namespace ######.iOS.Services
{
public class AccountService : IAccountService
{

    public AccountService()
    {
    }



    public Task<bool> SendOtpCodeAsync(string phoneNumber)
    {


        _phoneAuthTcs = new TaskCompletionSource<bool>();

        PhoneAuthProvider.DefaultInstance.VerifyPhoneNumber(
            phoneNumber,
            null,
            new VerificationResultHandler(OnVerificaitonResult));


        return _phoneAuthTcs.Task;

    }

    private string _verificationId;

    private void OnVerificaitonResult(string verificationId, NSError error)
    {

        if (error != null)
        {

            _phoneAuthTcs?.TrySetResult(false);
            return;
        }

        _verificationId = verificationId;
        _phoneAuthTcs?.TrySetResult(true);
    }

    public Task<bool> VerifyOtpCodeAsync(string code)
    {


        var tcs = new TaskCompletionSource<bool>();

        var credential = PhoneAuthProvider.DefaultInstance.GetCredential(_verificationId, code);


        Auth.DefaultInstance.SignInWithCredentialAsync(credential)
            .ContinueWith((task) => OnAuthCompleted(task, tcs));

        return tcs.Task;
    }

    private TaskCompletionSource<bool> _phoneAuthTcs;


    private void OnAuthCompeleted(Task task, TaskCompletionSource<bool> tcs)
    {

        if (task.IsCanceled || task.IsFaulted)
        {
              tcs.SetResult(false);
            return;
        }


        tcs.SetResult(true);
    }



    private void OnAuthCompleted(Task task, TaskCompletionSource<bool> tcs)
    {

        if (task.IsCanceled || task.IsFaulted)
        {
            tcs.SetResult(false);
            return;
        }



        tcs.SetResult(true);
    }


  }
}
반응형
댓글