开发者

Strategies for canonicalizing phone numbers from users' address books?

This is a general problem but is affecting me particularly on Android and iPhone: given a user and a phone number, h开发者_运维百科ow can I canonicalize that phone number to be actually useful for storing and dialing? A user could have a phone number in their address book of the forms:

  • 7-digit US number (555-1212)
  • 10-digit US number (210-555-1212)
  • International number with + (+46-555-1212)
  • In-country non-US full number (123-555-1212)
  • In-country non-US truncated number (555-1212)

Things I know about the user submitting this number:

  • IP address
  • maybe their phone number
  • maybe their selected country
  • maybe their selected region

This seems like a tough problem– I definitely don't want to ask the user for more information unless I really need to, but this data needs to be pretty trustworthy. Is there a best practice I can reuse here?


IOS

Ok, this just MIGHT be helpful to You. Let's hope so.

In my app I needed to - somehow get a telephone number from contacts. So the problem is as you explained - can be with various -*() chars, and with-without country codes.

So - I get contact number using ABPeoplePickerNavigationController and get from number true number and possible - country code using function:

- (void)saveContactPhone:(NSString *) mContactPhone
{
    if(mContactPhone && [mContactPhone length])
    {   
        if ([mContactPhone rangeOfString:@"+"].location != NSNotFound)//this means number includes country code. 
        {
            NSString * mCCodeString = @"";

            BOOL mFound = FALSE;

            for(int i = 0; i<[mContactPhone length]; i++) // check number for any obvious country code.
            {
                if(mFound == TRUE)
                {
                    break;
                }

                mCCodeString = [mContactPhone substringToIndex:i];

                mCCodeString = [[mCCodeString componentsSeparatedByCharactersInSet:
                [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] 
                componentsJoinedByString:@""];

                if([mCCodeString intValue] != 1)//because This is US/CA
                {
                    for(int j = 0; j<[pickerViewElementArray_ count]; j++)
                    { 
                        if([[pickerViewElementArray_ objectAtIndex:j] intValue] == [mCCodeString intValue])
                        {
                            mFound = TRUE;

                            //we got ourselves final telephone number 
                            //and we got country code!!

                            mContactPhone = [mContactPhone substringFromIndex:i];

                            break;
                        }
                    }
                }

            }

            if(mFound == FALSE)//If no luck finding a match - lets try again, but til index 2. (find if it is +1)
            {
                mCCodeString = [mContactPhone substringToIndex:2];

                mCCodeString = [[mCCodeString componentsSeparatedByCharactersInSet:
                [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] 
                componentsJoinedByString:@""];

                mContactPhone = [mContactPhone substringFromIndex:1];

                for(int i = 0; i<[pickerViewElementArray_ count]; i++)
                { 
                    if([[pickerViewElementArray_ objectAtIndex:i] intValue] == [mCCodeString intValue])
                    {   
                        //we found it!! Its +1!!!!

                        mFound = TRUE;

                        break;
                    }
                }
            }
        }
    }

    mContactPhone = [[mContactPhone componentsSeparatedByCharactersInSet:
            [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] 
            componentsJoinedByString:@""];
}

Also you need country code array: for example:

NSArray *pickerViewElementArray_ = [NSArray arrayWithObjects: 
    @"93",
    @"355",
    @"213",
    @"1684",
    @"376",
    @"244",
    ....

Hope that helps somebody!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜