<分区>
标签 iphone ios cocoa-touch
我有一个广播节目应用程序,其中包含许多美国电话号码。我收到一位国际用户的反馈,他提到需要将“+1”添加到号码中才能在国际上使用。处理这个问题的最佳方法是什么?
我可以将所有数字加 +1 吗?或者这会导致国内调用者产生奇怪的费用吗?
如果是这样,我是否需要添加偏好设置,或者是否有一些可靠本地化或地理信息可供我在需要时自动添加 +1?
最佳答案
在处理国际电话号码时,您必须使用所谓的 E164 格式,该格式描述了每个国家/地区电话号码的国际格式。在详细介绍之前,我想说,如果您在某个国家/地区(在本例中为美国),那么您是否添加 +1 并不重要,因为运营商会在调用电话时自动添加它。但当您前往世界各地时,为了安全起见,您需要采用 E164 格式。
我个人在我的一个应用程序中需要这样的东西,我做了一个个人函数,将电话号码作为字符串和国家/地区代码(也作为字符串)并返回该号码的 E164 格式。
为什么我要做这个功能?因为正如您所知,在设备上您可能会看到存储在不同布局中的数字,例如:
并且您需要先处理它们,然后才能使用它们。我的函数不考虑这种格式的号码:+44-(0)78-1234-1234,但如果需要,您可以添加它。
这是我的功能:
- (NSString *) phoneToE164:(NSString *)originalString CountryCode:(NSString *)countryCode
{
//strip the string from symbols
NSString *strippedString = [[originalString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
NSMutableString *finalString = [NSMutableString stringWithString:strippedString];
//if the first character is + your done
if( [originalString characterAtIndex:0] == '+' )
{
[finalString insertString:@"+" atIndex:0];
}
//if the first 2 characters are 00 replace them by +
else if( ([finalString characterAtIndex:0] == '0') && ([finalString characterAtIndex:1] == '0') )
{
[finalString deleteCharactersInRange:NSMakeRange(0, 2)];
[finalString insertString:@"+" atIndex:0];
}
//if the first character is 0 replace it with country code
else if( [finalString characterAtIndex:0] == '0' )
{
[finalString deleteCharactersInRange:NSMakeRange(0, 1)];
[finalString insertString:countryCode atIndex:0];
}
//anything else is unknown format, just insert the country code
else if( [finalString characterAtIndex:0] != '0' )
{
[finalString insertString:countryCode atIndex:0];
}
//return the result
return(finalString);
}
顺便说一下,这个功能不仅适用于美国,还适用于任何国家,只需将您想要的国家代码发送给它即可。
关于iphone - 从海外调用美国电话的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9153203/