UIAlertController的约束

时间:2018-01-28 13:24:24

标签: ios swift firebase firebase-authentication uialertcontroller

我创建了一个应用,我使用FirebaseAuth和电话号码。我制作了一个UIAlertController,我必须在其中输入验证码,它将Firebase发送到我的手机号码:

Alert Image

约束很酷,但在那之后,我得到一个新的ViewController(Recaptcha验证):

Recaptcha verification

当我点击确认时,它解除了我的确认警报,但警报的限制被打破了:

enter image description here

func showVerificationAlert() {
    let alertController = UIAlertController(title: "Verification code", message: "Please put here your verification code", preferredStyle: .alert)
    alertController.addTextField { (textField) in
        textField.placeholder = "Verification code"
    }
    let alertAction = UIAlertAction(title: "Ok", style: .default, handler: { alert in
        let credential = PhoneAuthProvider.provider().credential(withVerificationID: self.self.verificationId!, verificationCode:  alertController.textFields![0].text!)
        Auth.auth().signIn(with: credential, completion: { (user, error) in
            if let error = error {
                debugPrint(error)
                return
            }
            print("userSignedIn")
            animateFromRight(viewControllerToPush: MainScreenVC())
        })
    })
    let alertActionCancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
    alertController.addAction(alertActionCancel)
    alertController.addAction(alertAction)
    present(alertController, animated: true, completion: nil)
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

似乎您的警报因某些原因不会被解雇。请尝试以下方法:

func showVerificationAlert() {
    let alertController = UIAlertController(title: "Verification code", message: "Please put here your verification code", preferredStyle: .alert)
    alertController.addTextField { (textField) in
        textField.placeholder = "Verification code"
    }
    let alertAction = UIAlertAction(title: "Ok", style: .default, handler: { alert in
        let credential = PhoneAuthProvider.provider().credential(withVerificationID: self.self.verificationId!, verificationCode:  alertController.textFields![0].text!)

        // lets try to explicitly dismiss it
        alertController.dismiss(animated: false, completion: nil)

        Auth.auth().signIn(with: credential, completion: { (user, error) in
            if let error = error {
                debugPrint(error)
                return
            }
            print("userSignedIn")
            animateFromRight(viewControllerToPush: MainScreenVC())
        })
    })
    let alertActionCancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
    alertController.addAction(alertActionCancel)
    alertController.addAction(alertAction)
    present(alertController, animated: true, completion: nil)
}