0

For my application I need a common formatting scheme for phone Numbers. Unfortunately the numbers in the phone Book do not share the same scheme. E.g.

  • (707) 555-1854
  • 555-610-6679
  • 06641234567
  • +43 664 1234567
  • 00436641234567

Is there some kind of number formatter on the iOS platform which can transform these numbers into a scheme looking like this:

+'CountryCode''Number'

How do i get the right country code for local numbers?

Thanks for your help.

dominik
  • 1,319
  • 13
  • 23
  • 1
    It seems highly unlikely that there *is* a way at all. Otherwise what would be the purpose of country codes? – borrrden Jul 16 '13 at 08:36
  • Well I need to hash the numbers, so I need a common scheme for all the numbers. I think the country code is necessary, because if I have the following numbers: +43 123 456 789 and +1 (123) 456 789 And would ignore the country code, both numbers would represent the same hash.. – dominik Jul 16 '13 at 08:40
  • Additionally the hash should be identical for numbers looking like this: +43 123 456 789 and 0043 123 457 789.. So I need some kind of formatting which takes care of that. – dominik Jul 16 '13 at 08:43
  • 1
    That's what I am saying. If there were a way to get the country out of the phone number, then the country code wouldn't be needed in general. The country code will help break ambiguity between two phone numbers so what you are trying to do doesn't seem possible. – borrrden Jul 16 '13 at 08:46
  • Yes of course. Based on the number there is no possibility to get the country code if there isn't any. But that means, that this number, has to be a local number, otherwise it would not be possible to call it. So is there a possibility to access the SIM carrier (not the current carrier, because if I'm abroad the Carrier will change).. – dominik Jul 16 '13 at 08:49
  • No, this is a violation of the user's privacy and Apple does not provide an API to access it. If you persist and try to use private API information then you will most likely be rejected from the App Store. – borrrden Jul 16 '13 at 08:51
  • Ok, but I could use something like NSLocale to obtain the current locale, and then transfer it to the phone country code.. – dominik Jul 16 '13 at 09:04
  • Actually your previous comment is not true. I can get the SIM Carrier using CTTelephonyNetworkInfo.. – dominik Jul 16 '13 at 09:21
  • Oh, nice! I didn't know that. Beware though, because it won't work in airplane mode, or outside cellular range, so you need to have a backup plan. `NSLocale` will return the locale that the user has selected in their preferences, so it may not match (for example, I live in Japan but my phone is set to English). – borrrden Jul 16 '13 at 09:25

1 Answers1

0

While not a perfect match for what you're looking for, there is an open source library that might help: RMPhoneFormat (https://github.com/rmaddy/RMPhoneFormat). It makes use of a binary file PhoneFormats.dat from Apple to format numbers based on a given locale. Formatting a number is simple as:

RMPhoneFormat *fmt = [[RMPhoneFormat alloc] init];
NSLog(@"num: %@", [fmt format:@"7075551854"]); // outputs (707) 555-1854 for US locale

It works with country codes as well, so if you add a +1 to numbers you know are in the US, these numbers will be formatted like this: +1 (707) 555-1854. For European numbers beginning with zero, you could trim the leading 0 and add the appropriate country code.

newenglander
  • 2,019
  • 24
  • 55