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?
相关问题
- Correctly parse PDF paragraphs with Python
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- How do I get from a type to the TryParse method?
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
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.
After some researches I found this lib which seems to do the work: https://github.com/iziz/libPhoneNumber-iOS
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.
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:We cannot identify the country code from the phone number.