Search code examples
iosswift3xcode8twitter-digitsfabric-digits

How to get user's phone number?


I have just started using Digits - Twitter API for Phone Number verification, but it seems I'm unable to read the user's Phone number, I'm not sure if there is a function for that or so, but after reading a while I knew that I can do that with a Call back after successful phone verification but no explanation for that !

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

found this snippet but again not sure where to implement it.

HERE is my Action for the login button with phone verification:

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")
        }
    }

}

Solution

  • So I found the answer after digging a lot more in Digits Documentations and it was pretty simple, I had to add:

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

    In the didTap function, so the complete code will be:

    @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")
            }
        }
    
    }
    

    Here is the Reference I have used: https://docs.fabric.io/apple/examples/cannonball/index.html#sign-in-with-digits