Xamarin表单的Xamarin Firebase PhoneAuth

时间:2018-08-21 07:04:45

标签: android firebase xamarin xamarin.forms firebase-authentication

我需要创建一个具有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”

1 个答案:

答案 0 :(得分:0)

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

const myData = [
  {
    id: '1',
    nameHeader: 'Header1',
    subMenu: [{ id: '1', name: 'subMenu1' }, { id: '2', name: 'subMenu2' }]
  },
  {
    id: '2',
    nameHeader: 'Header2',
    subMenu: [{ id: '1', name: 'subMenu1' }, { id: '2', name: 'subMenu2' }]
  }
]

class Myclass extends Component {
  state = { open: false }

  handleClick = () => {
    this.setState(state => ({ open: !state.open }))
  }

  render() {
    const { open } = this.state
    return (
      <div style={{ marginRight: '15px' }}>
        <List component="nav">
          {myData.map(each => (
            <React.Fragment key={each.id}>
              <ListItem button onClick={this.handleClick}>
                <ListItemText inset primary={each.nameHeader} />
                {open ? <ExpandLess /> : <ExpandMore />}
              </ListItem>
              <Divider />
              <Collapse in={open} timeout="auto" unmountOnExit>
                <List component="div" disablePadding>
                  {each.subMenu.map(subData => (
                    <ListItem key={subData.id} button>
                      <ListItemText inset primary={subData.name} />
                    </ListItem>
                  ))}
                </List>
              </Collapse>
            </React.Fragment>
          ))}
        </List>
      </div>
    )
  }
}
export default Myclass

}

以及您的依赖服务实现

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;

    }