I'm able to retrieve contact's phone number from the device using Address Book framework on iOS. How can I separate or identify which is country code and which is actual phone number? Is it possible?
问题:
回答1:
Country codes are a fairly messy topic but standard enough that with some programmer maintenance you can solve it.
You can identify a country code by its initial subsequence. Do the following algorithm:
Build a simple database - can even store as a flat file and load into a dictionary in memory - of country codes.
Recursively from n = 1 to 3, check if the first n digits of the phone number match any country code. If so, that's your country code. If it fails by try 3, it's not a valid country code.
If you like deal with quasi-ambiguities. If you ever see overlap between country codes, e.g. if you see "1340" for Virgin Islands, it's because in some probably irrelevant sense it's a subset of the country code "1" for U.S. Country codes sometimes reflect geopolitical situations and usually this is expressed by splitting the country code to the next digit. "1" will be the official country code but which is of more interest to you depends on your application.
Note: This takes maintenance when thinks like the Soviet Union breakup happen.
Now, things get messier when you have to deal with the fact that some #s already have country codes on them and some don't and such. The only country code I've found in which the country code can also occur as a leading area code is Singapore - I think it was +65-65...
. With decent accuracy you can use business logic along the lines of:
- Watch for U.S. number format: (xxx) xxx-xxxx and such.
- Use the fact that only certain digits lengths can occur in given countries. There are documents on this if you search hard enough. e.g., all U.S. numbers are length 10, Japanese numbers can be length 9 or 10. This can give you added information.
- Ignore leading 0's for most countries. Italy and Ivory Coast are the only two exceptions I've found, these can have valid leading 0's. Otherwise it's just a local telecom convention for staying within the country, usually.
回答2:
What I did was to create an array of all the country calling codes on wikipedia. Then, when I have a phone number, I try to match the first four digits with one of the country calling codes. If none match, try the first three digits and so on. Eventually I find the calling code.
回答3:
After some researches I found this lib which seems to do the work: https://github.com/iziz/libPhoneNumber-iOS
回答4:
Country calling codes are fairly easy to detect from phone number.
The first thing you need to do is to parse calling codes into a tree where each digit is representing a branch.
Then, finding a country code is as easy as traversing the tree, digit by digit, until the last branch which is a country code match.
At this moment (July 2016.), the longest country calling code has 5 digits, so your tree should not have more than 5 levels of branches.
回答5:
We cannot identify the country code from the phone number.