0

I have a KMM project (and am working in the Shared>iOSMain module), and need to read the device contacts, and send phone numbers back to a server in E164 format (actually the number is hashed before being sent back, but it must be in E164 prior to the hashing).

I have this working fine on the Android side, and am reading the contacts on the iOS side, but my problem is that all I can get from iOS is CNPhoneNumber.stringValue which is a formatted phone number like (888) 555 5512, and not E164 (+18885555512).

I cant find any easy way to convert this to E164, as the stringValue may or may not have country code in it, and there's no way to tell (and this needs to work globally).

Surely getting phone numbers from contacts into the international standard must be straight forwards - what am I missing?

Charlie Walker
  • 129
  • 1
  • 2
  • 9
  • 1
    Unfortunately it isn't straightforward. Ideally users would store their phone numbers in e.164 format, but if they don't then you need to try and convert them. The contacts framework just gives you whatever the user has put on the contact. [PhoneNumberKit](https://github.com/marmelroy/PhoneNumberKit) is a popular Swift framework to help with this, but your still need to take a "guess" as to the user's home country – Paulw11 Jul 22 '23 at 05:26
  • I'm in a similar positon wanting a common kotlin phone formatter. I just started porting https://github.com/MichaelRocks/libphonenumber-android to common kotlin if you want to help out with that. https://github.com/luca992/libphonenumber-kotlin – luca992 Jul 25 '23 at 23:04

1 Answers1

0

Check out my port of kotlin multiplatform fork of libphonenumber! https://github.com/luca992/libphonenumber-kotlin

Besides switching to using kotlin Regex vs java libs it's basically a direct port where most of the code was converted with the java to kotlin converter. So there's probably lots of room for improvement and there probably are some bugs I haven't discovered. But it's working in the include compose multiplatform sample!

You can then do something like this for formatting to E164.

luca992
  • 1,548
  • 22
  • 27