Dealing with international phone numbers on iPhone
I have an app which uses contact data in the following ways:
- To send an SMS from the client
- To send an SMS from our server
- To upload a user's phone number to a database where others can match against it
This works fine in the U.S. but internationalizing has a set of issues. These are my assumptions for making it work:
#1 - Sending an SMS from the client is straightforward. Use the number in the address book verbatim (without normalization) and let the OS figure it out. Chances are if they can make a phone call via a given number they can send an SMS (right?)
#2/#3 - Sending an SMS from the server is more complicated. Most SMS APIs require international notation (the "+" prefix and country code) whereas the user will have in their contact list a variety of various local and global phone number with various prefixes (area code, country code, etc). So normalization would be required here. Matching a user's phone number in a database also requires normalization.
To normalize phone numbers, I have to first decide if the user meant to specify a global number by looking for "+" "011" and "0开发者_Go百科0" prefixes. If so, I remove the prefix, replace it with "+" and use that number.
If I think the number is local, I can try to tack on the country code myself (heuristic, not reliable). This requires knowing the user's default country code. On the iPhone, I can get the user's preferred region (which is selectable) and the user's MCC and MNC (dependent on location?). I am leaning toward the user's selected country code because chances are most of their local phone numbers will be of that country.
I believe this is the best I can do without parsing the entire number and figuring out area code and other components. I assume most countries allow "0" as a prefix for their own country code.
Make sense? Is there an easier/more sane way to do this?
#1 this is a fair assumption, except when the user goes overseas.
#2/#3 To do a more reliable normalisation, you need to get the user's coarse location. Here's the psudeo code:
- if number starts with "+", leave it as is
- else get user's current country (refer to this post):
- if tarts with country exit codes, replace that with "+"
- if starts with country code, add a "+"
- if starts with one "0", replace that with "+"
- else, assume it's local number, add an area code and country code. With the complexity of this one, it might be easier to prompt the user for some input, e.g. their current area code and country code, or their location.
精彩评论