2

If I have a contact in my iPhone address book of the form XXX-YYYY and tap it to dial it then the iPhone dials the number 206-XXX-YYYY (206 is the area code for Seattle, US, which is where the phone doing the dialing and the phone being dialed both are).

I have an app which goes through a user's Contact's app extracting the phone numbers and I would like to be able to detect when an area code is missing like this and to add it in the same was as iOS itself is doing.

Does anybody have any idea how I could do this?

I know the iPhone's phone number, which will be for example AAA-BBB-CCCC, so I could compare AAA-BBB-CCCC with the phone numbers in the address book and if I see a number like XXX-YYYY then I could deduce a) that the area code is missing and b) that the missing area code must be the same as the iPhone's area code and so prepend AAA to XXX-YYYY.

I know that's going to be too simplistic a solution, and might only work in North America.

So how is the iPhone doing it, and is there a scalable solution that will work for all countries?

I'm aware of libPhoneNumber (and an iOS port of it https://github.com/iziz/libPhoneNumber-iOS) but looking through its API I can't see anything that will firstly detect if an area code is missing from a phone number. Maybe its there, but the iOS libPhoneNumber port isn't extensively documented.

Anybody faced and solved a similar issue to this?

Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • "the missing area code must be the same as the iPhone's area code"—is that true? Do you have geographic area codes for mobile phones in the US? Or is the phone/network just being smart enough to recognise that if you dial a number without an area code, it should use the area code that the phone happens to be in at the moment (as opposed to the actual area code assigned to the phone)? To put it another way, if you took your phone from Seattle to New York, and tried that number again, would you end up dialling a Seattle number, or a New York number?) – Matt Gibson Jan 15 '15 at 16:36
  • I don't know if its the iPhone or the network which is doing the guessing. If I enter a New York number without area code and dial from Seattle then it adds a Seattle area code, if I physically go to New York I don't know if it will use the current area code of the device or will continue to use the Seattle area code. However for my implementation I will have to choose one, even though it might not be %100 perfect, I can use the sim's area code assuming their contacts without area code are their local contacts. It wouldn't be possible to obtain the current area code AFAIK anyway. – Gruntcakes Jan 15 '15 at 17:03
  • It definitely won't work in the UK, by the way. But then we're less likely to have phone numbers without area codes stored in our mobile phones, as they won't be dialled correctly anyway. – Matt Gibson Jan 15 '15 at 17:17
  • Phone dialing is going to work like traditional landline phones when you enter a phone number without an area code. Meaning that it will assume the area code of the network you are connected to unless you say otherwise. Also, area codes are not purely geographical. Most major metropolitan areas have multiple area codes just because there aren't enough numbers in one code to serve the area. Dialing in those areas REQUIRES that the user enter an area code. In large cities it will not be possible to auto fill an area code. It will be problematic on many levels to assume an area code – Dancreek Jan 19 '15 at 16:56
  • So if I have a contact in the address book without an area code and it successfully dials the correct number, its basically luck that it worked? (Or rather that the dialed number has the same area code as the connected network). – Gruntcakes Jan 19 '15 at 17:04
  • I think it's the network doing the guessing. For what it's worth, I'm on Sprint (iOS 8.1.2) and my phone did not autocomplete the area code when I dialed a number without one. Maybe it also depends on where your area code is from; my area code is one which requires area codes to be dialed even on landlines. – strange quark Jan 20 '15 at 04:24
  • Quick question : Do you support international number or is it a US only area code thingy? /EDIT / Nervermind, got my answer. – Florian Burel Jan 23 '15 at 11:22

3 Answers3

1

How can we detect telephone area code? If number have dial code then dial code always in start position of the number string. Maximum string length of area code is 4. and minimum string length is 1. We need to create recursive logic to check first 4 char string. Start with substring with length 1. And compare with array of dial code. If array of dial code haven't substring object. then increase length of dial code recursively. Max substring length is 4. If array contain the substring object then break the recursive loop. That substring is telephone code.

You need to create one static array for all countries code. You can find all countries code from here

Pradip Vanparia
  • 1,742
  • 10
  • 22
  • What if the area code is 206. Then what if a full valid number is 206 206 1234 (the area code is 206 and the remainder of the number is 2061234) . Then if we want to evaluate if 206 1234 has the area code missing or not, your algorithm will incorrectly determine that the number has the area code, but it does not. – Gruntcakes Jan 23 '15 at 18:56
  • You want area code for number 206 206 1234. Then check first number "2" if the number "2" is country code then break the loop. If not then check "20". "20" is area code for country "Egypte". So, number 206 206 1234 area code is "20". – Pradip Vanparia Jan 28 '15 at 11:38
1

Did you consider using a reggae like in this post: Parse Phone Number into component parts

I'm not saying it will work for international solution, but at least it's a start

Community
  • 1
  • 1
Florian Burel
  • 3,408
  • 1
  • 19
  • 20
1

Unfortunately, numbers with missing area codes cannot be determined in many cases. You can do some things to help the odds.

First, you could make the assumption that the 7-digit numbers are in the same area code as the user's phone number and just append the area code.

Second, since many U.S. locations are served by multiple area codes (e.g., Denver has area codes 303 and 720), you could check to see if the combination of area code and prefix is valid. Several services and database contains this information, such as http://www.ccmi.com/npa-nxx-rate-center-lookup. (Note that the area code is referred to as the NPA and the prefix is referred to as the NXX.) The NPA-NXX combination can tell you the location and usually the carrier and telecom switch it belongs to.

Third, if your reading the address information along with the phone numbers, then you have a distinct advantage. Since the NPA-NXX is location-specific, you can use that location data to better validate the correct area code.

None of this is perfect, and there will be mistaken assumptions made. One big reason is number portability. For example, I can take my Denver phone number with me when I move or change carriers.

picciano
  • 22,341
  • 9
  • 69
  • 82