PhoneNumberKit icon indicating copy to clipboard operation
PhoneNumberKit copied to clipboard

Some UK (GB) numbers are guessing a very weird region, destroying live formatting

Open yspreen opened this issue 3 years ago • 11 comments

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 avatar Aug 04 '21 09:08 yspreen

@yspreen did you find a solution for this? I've updated to the latest package but no joy...

michaelmoneypenny avatar Mar 31 '22 12:03 michaelmoneypenny

@michaelmoneypenny I honestly don't remember. let me check my code from back then

yspreen avatar Mar 31 '22 16:03 yspreen

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

yspreen avatar Mar 31 '22 16:03 yspreen

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.

github-actions[bot] avatar May 31 '22 03:05 github-actions[bot]

Up, would this issue be resolved?

christianYoopies avatar Nov 28 '23 16:11 christianYoopies

it won't

yspreen avatar Nov 28 '23 16:11 yspreen

I guess it was already

bguidolim avatar Nov 28 '23 16:11 bguidolim

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

christianYoopies avatar Nov 29 '23 10:11 christianYoopies

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.

bguidolim avatar Dec 04 '23 13:12 bguidolim

thanks! exactly the answer i was looking for 2y ago 😄

yspreen avatar Dec 04 '23 16:12 yspreen

I'm sorry that it took so long 😑

bguidolim avatar Dec 04 '23 16:12 bguidolim

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.

github-actions[bot] avatar Feb 03 '24 03:02 github-actions[bot]