Canonization strategies for phone numbers from user address books?

This is a common problem, but it affects me especially on Android and iPhone: given the user and phone number, how can I canonicalize this phone number, which is really useful for storing and dialing? A user can have a phone number in his address book of forms:

  • 7 digit US number (555-1212)
  • 10 digit U.S. number (210-555-1212)
  • International number with + (+ 46-555-1212)
  • Full domestic number (123-555-1212)
  • Truncated Domestic Number (555-1212)

Things I know about the user sending this number:

  • IP address
  • maybe their phone number
  • perhaps their chosen country
  • perhaps their chosen area

This seems like a difficult problem - I definitely do not want to ask the user for more information if I really do not need it, but this data should be reliable enough. Is there any best practice that I can reuse here?

+4
source share
1 answer

IOS

Well, that just CAN be useful to you. I hope so.

In my application, I needed to somehow get the phone number from the contacts. Thus, the problem, as you explained, can be with different characters - * () and with no country codes.

So - I get the contact number using ABPeoplePickerNavigationController and get the true number and the possible country code from the number using the 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:@""]; } 

You also need an array of country code: for example:

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

Hope someone help!

+1
source

Source: https://habr.com/ru/post/1348197/


All Articles