PhoneNumberKit
PhoneNumberKit copied to clipboard
Some UK (GB) numbers are guessing a very weird region, destroying live formatting
Very simple case. If I enter
+44 742405
It gets formatted as
+44 1481 742405
Assuming the region JE for some reason. I've been banging my head at the wall for hours now, trying to get a solution for this. It seems like the library goes alphabetically when trying to find the region instead of by length? Other than going through a dictionary to replace all regions that start with +44 by "GB" I have no idea how to solve this
@yspreen did you find a solution for this? I've updated to the latest package but no joy...
@michaelmoneypenny I honestly don't remember. let me check my code from back then
yeah, it seems I did find a workaround. going by length, and by prefix. only GB seems to have this issue. the project is kinda dead so I'll just post the code here. if you want you can follow me on github as a little thankyou :P
//
// Phone.swift
// Bubble
//
// Created by Yannick Spreen on 6/30/21.
//
import Foundation
import PhoneNumberKit
import CoreTelephony
enum Phone {
static let kit = PhoneNumberKit()
static let region = PhoneNumberKit.defaultRegionCode()
public static var currentCountryCode: String {
return "+\(kit.countryCode(for: region) ?? 1)"
}
public static func parse(_ text: String, toType: PhoneNumberFormat = .international) -> String? {
let raw = String(text.filter { "+0123456789".contains($0) })
if raw.starts(with: "+44"), raw.count < 3 + 9 {
return nil
}
guard let num = try? kit.parse(text, withRegion: region) else {
return nil
}
let parsed = kit.format(num, toType: toType)
return parsed
}
}
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.
Up, would this issue be resolved?
it won't
I guess it was already
@bguidolim I tried getting the latest version 3.7.5
of the library and I still need to use a workaround similar to proposed here because:
log.debug("phone before \(phone)")// prints "phone before +441231 23"
let phoneNumber = try? phoneNumberKit.parse(phone, withRegion: countryCode.value ?? "") {
log.debug("regionId identified \(phoneNumber.regionID!)")// prints "regionId identified JE"
let phoneFormatted = PartialFormatter(phoneNumberKit: phoneNumberKit,
defaultRegion: phoneNumber.regionID ?? "",
withPrefix: false).formatPartial(String(phoneNumber.nationalNumber) ?? "")
log.debug("phone after \(phoneFormatted)")// prints "phone after 1534 123123"
I've taken a look at this issue, and it appears to me to be a misuse of the library. You should not rely on any results from phoneNumberKit.parse
if the phone number is not considered complete. This function is intended to be used when you have the final input from the user.
For live formatting, you should use PartialFormatter
. This is similar to the example code AsYouType
, where this issue doesn't occur.
thanks! exactly the answer i was looking for 2y ago 😄
I'm sorry that it took so long 😑
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.