+1856 US PhoneNumber parsed as Barbados Number
+18567797 will formatted as +1 246-856-7797
let phoneNumberKit = PhoneNumberKit() do { let phoneNumber = try phoneNumberKit.parse("+18567797") //part of the phone number +18567797XXXX let formatted = phoneNumberKit.format(phoneNumber, toType: .international) print(formatted) //+1 246-856-7797 } catch { print("Generic parser error") }
(PhoneNumberKit.PhoneNumber) phoneNumber = { numberString = "+18567797" countryCode = (_value = 1) leadingZero = false nationalNumber = (_value = 2468567797) numberExtension = nil { some = { _guts = { _object = (_countAndFlagsBits = Swift.UInt64 @ 0x000000016dcf1240, _object = 0x0000000000000000) } } } type = mobile {} regionID = "BB" }
I can't reproduce the issue on the current version (3.4.5). You can find a video with steps to reproduce (using "AsYouType" example project) below.
https://user-images.githubusercontent.com/14940890/185020588-a1a44cde-9990-443c-9730-d16ffe11ee4c.mp4
I tested with the current version (3.4.5) with the same result.
- my number contains the prefix "+"
let phoneNumberKit = PhoneNumberKit() do { let phoneNumber = try phoneNumberKit.parse("+18567797") //part of the phone number +18567797XXXX let formatted = phoneNumberKit.format(phoneNumber, toType: .international) print(formatted) //+1 246-856-7797 } catch { print("Generic parser error") }
Please use the above code with the Xcode debugger and you will see the same result.

Summary
When we pass incomplete US phone number, the system will think that it is the Barbados phone number.
Debug
When we call parse(_ numberString: String, withRegion region: String, ignoreType: Bool) throws -> PhoneNumber method (can be found in ParseManager.swift) with the provided part of the number (+18567797) we will eventually enter if let result = try validPhoneNumber(from: nationalNumber, using: regionMetadata, countryCode: countryCode, ignoreType: ignoreType, numberString: numberString, numberExtension: numberExtension) check.
For this check, we are passing a lot of properties, but the most important is regionMetadata (MetadataTerritory).
This property has a lot of important data on how a targeted number should behave. The most important one (for our ticket) is generalDesc (MetadataPhoneNumberDesc) that contains nationalNumberPattern (String).
This nationalNumberPattern is a simple regex. In our case it is equal to "[2-9]\\d{9}|3\\d{6}"
Within validPhoneNumber method we are failing at if let generalNumberDesc = regionMetadata.generalDesc, regexManager.hasValue(generalNumberDesc.nationalNumberPattern) == false || parser.isNumberMatchingDesc(nationalNumber, numberDesc: generalNumberDesc) == false line. Specifically parser.isNumberMatchingDesc(nationalNumber, numberDesc: generalNumberDesc) == false check is failing.
It fails because a passed number and nationalNumberPattern do not have a 100% match.
Since we have failed the validPhoneNumber for the US code, we are starting to search for another country that could fit the requirement.
Unfortunately (or luckily), Barbados has a match.
Fix
If my understanding is correct, we can't fix the issue since it based on the regex that we have zero control on.
@bguidolim @petermolnar-dev let me know if my conclusion is not correct. Will be happy to be wrong!
I encountered this issue over a year ago. I cannot offer much to the discussion, but in case it is helpful to others I commented in code that V3.3.1 of PhoneNumberKit does not exhibit the issue.
/// IMPORTANT NOTE:
/// 4/22/21 PhoneNumberKit is hardwired to v3.3.1
/// v3.3.3 was found to unexpectedly add the '246' area code (Barbados) when formatting a partial 7-digit number
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.