如何使用swift在firebase中添加手机身份验证?

时间:2017-05-25 15:47:28

标签: ios swift firebase firebase-authentication

所以firebase最近支持手机验证,但问题是文档是在objective-c。我之前从未做过推送通知。这是objc phone auth docs的链接:https://firebase.google.com/docs/auth/ios/phone-auth

1 个答案:

答案 0 :(得分:2)

您可以在官方样本仓库中看到一个示例:https://github.com/firebase/quickstart-ios/blob/master/authentication/AuthenticationExampleSwift/MainViewController.swift#L161

核心流程如下:

PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber) { (verificationID, error) in
    if let error = error {
      // TODO: show error
      return
    }
    guard let verificationID = verificationID else { return }
    verificationCode = // TODO: get SMS verification code from user.
    if let verificationCode = verificationCode {
      let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID, verificationCode: verificationCode)
      Auth.auth().signIn(with: credential) { (user, error) in
        // TODO: handle sign in
      }
    } else {
      // Verification code was empty
    }
  }
}

您需要插入用户界面以提示用户(示例中的内容),或利用FirebaseUI为您处理所有内容:https://github.com/firebase/FirebaseUI-iOS