Emad
Emad

Reputation: 366

Getting the Phone Country Code from users phone number in swift

Hi I have an IOS application in which the user can use Digists to authenticate.

When you authenticate with Digists you get an String value of the phone number.

I would like to know if there is any way I can find out the Locale/phone code of the user using this phone number string.

For example: Lets say the user has some French sim card number and goes to Great Britain. Then he uses Digits to authenticate in our app.

Lets assume his phone number is +33509758351. Is there a way to find out that the phone code for the phone number +33509758351 is +33 or find out that the Local for that phone number is FR?

I know there are libraries like PhoneNumberKit which can be used to validate a phone number against a locale, but I cannot seem to find one that will give me the user's Locale/Phone Code based on phone number string.

Upvotes: 2

Views: 3780

Answers (1)

TMoon
TMoon

Reputation: 21

Try this code:

let phone : String = "your phone with carrier region"
let phonUtil = NBPhoneNumberUtil()
do {
    let nbPhone : NBPhoneNumber = try phonUtil.parse(withPhoneCarrierRegion: phone)
    var countryCode : NSNumber = phoneStep1.countryCode
}catch{
    //Phone Number Invalid!
}

Upvotes: 2

Related Questions