如何获取用户的phone号码?

How to get user's phone number?

我刚刚开始使用 Digits - Twitter API 进行 Phone 号码验证,但我似乎无法读取用户的 Phone 号码,我不确定如果有这样的功能,但在阅读了一段时间后我知道我可以在成功 phone 验证后通过回调来做到这一点,但没有解释!

AuthConfig.Builder authConfigBuilder = new AuthConfig.Builder()
                 .withAuthCallBack(callback)
                 .withPhoneNumber(phoneNumberOrCountryCodeFromMyActivity)

找到了这个片段,但又不确定在哪里实现它。

这是我对带有 phone 验证的登录按钮的操作:

fileprivate func navigateToMainAppScreen() {
    performSegue(withIdentifier: "signedIn", sender: self)
}

@IBAction func tapped(_ sender: Any) {

    let configuration = DGTAuthenticationConfiguration(accountFields: .defaultOptionMask)

    configuration?.appearance = DGTAppearance()
    configuration?.appearance.backgroundColor = UIColor.white
    configuration?.appearance.accentColor = UIColor.red

    // Start the Digits authentication flow with the custom appearance.
    Digits.sharedInstance().authenticate(with: nil, configuration:configuration!) { (session, error) in
        if session != nil {
            // Navigate to the main app screen to select a theme.
            self.navigateToMainAppScreen()

        } else {
            print("Error")
        }
    }

}

所以我在 Digits 文档中进行了更多挖掘后找到了答案,它非常简单,我不得不添加:

print(session.phoneNumber)
print(session.userID)

在didTap函数中,完整代码为:

@IBAction func tapped(_ sender: Any) {

    let configuration = DGTAuthenticationConfiguration(accountFields: .defaultOptionMask)

    configuration?.appearance = DGTAppearance()
    configuration?.appearance.backgroundColor = UIColor.white
    configuration?.appearance.accentColor = UIColor.red

    // Start the Digits authentication flow with the custom appearance.
    Digits.sharedInstance().authenticate(with: nil, configuration:configuration!) { (session, error) in
        if session != nil {

            //Print Data
            print(session?.phoneNumber)
            print(session?.userID)

            // Navigate to the main app screen to select a theme.
            self.navigateToMainAppScreen()

        } else {
            print("Error")
        }
    }

}

这是我用过的参考: https://docs.fabric.io/apple/examples/cannonball/index.html#sign-in-with-digits