I am a new swift developer. I am using Swift 4.2 and Xcode 10.1.
I am parsing numbers using Mamelroy's PhoneNumberKit to return the country code. The parsing works fairly well (though a little slow at first). However, when given a number (888) or (866), toll free numbers in the US, PhoneNumberKit returns AG which is the two-digit country code for Antigua and Barbuda. The NPA prefix for AG is 268, which is not even close to 888 or 866.
I am prepared to handle a result of AG and test to see if it really is an Antigua or Barbuda number, but I'd like to confirm this is a problem with PhoneNumberKit and not something I'm doing wrong. My code is below.
class ContactService {
static let phoneNumberKit = PhoneNumberKit()
static func getFlag(number:String) -> String {
// Strip out all characters but +
let basicNumber = number.filter("01234567890+".contains)
// Get the country code and return it
do {
let phoneNumber = try phoneNumberKit.parse(basicNumber)
let regionCode = phoneNumberKit.getRegionCode(of: phoneNumber)
return regionCode!
}
catch {
return "PhoneNumberKit was unable to get the region code."
}
}
}
Three numbers that are returning AG in my contacts are the following:
1 (866) 814-9582, (866) 255-9679 and (888) 888-7020
Please let me know if this is a known issue or if there is another fix.
I found an answer here (though I had searched GitHub previously): https://github.com/marmelroy/PhoneNumberKit/issues/135. It says that AG (a US protectorate) is the first country that matches with toll free numbers, so PhoneNumberKit returns AG. It recommends returning the user's locale instead.
I suggest returning US.
I am leaving this question and answer here to assist others searching SO with this unexpected result.