【问题标题】:Strategies for canonicalizing phone numbers from users' address books?从用户通讯录中规范化电话号码的策略?
【发布时间】:2011-08-05 23:56:44
【问题描述】:
这是一个普遍问题,但在 Android 和 iPhone 上对我的影响尤其严重:给定一个用户和一个电话号码,我怎样才能规范化该电话号码以实际用于存储和拨号?用户可以在他们的表单地址簿中有一个电话号码:
- 7 位美国号码 (555-1212)
- 10 位美国号码 (210-555-1212)
- 国际号码 + (+46-555-1212)
- 国内非美国完整号码 (123-555-1212)
- 国内非美国截断号码 (555-1212)
我对提交此号码的用户的了解:
- IP 地址
-
也许他们的电话号码
-
也许他们选择的国家
-
也许他们选择的地区
这似乎是一个棘手的问题——除非我真的需要,否则我绝对不想向用户询问更多信息,但这些数据需要非常可靠。有没有我可以在这里重复使用的最佳实践?
【问题讨论】:
标签:
android
ios
mobile
phone-number
【解决方案1】:
IOS
好的,这可能对您有所帮助。希望如此。
在我的应用程序中,我需要 - 以某种方式从联系人那里获取电话号码。所以问题正如您所解释的那样 - 可以使用各种 -*() 字符,并且可以使用 - 不使用国家/地区代码。
所以 - 我使用 ABPeoplePickerNavigationController 获取联系号码并从号码中获取真实号码和可能的 - 使用函数的国家/地区代码:
- (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:@""];
}
还需要国家代码数组:例如:
NSArray *pickerViewElementArray_ = [NSArray arrayWithObjects:
@"93",
@"355",
@"213",
@"1684",
@"376",
@"244",
....
希望对某人有所帮助!