iphone - 从海外调用美国电话的最佳方式?

标签 iphone ios cocoa-touch

<分区>


想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。

已关闭10 年前

我有一个广播节目应用程序,其中包含许多美国电话号码。我收到一位国际用户的反馈,他提到需要将“+1”添加到号码中才能在国际上使用。处理这个问题的最佳方法是什么?

我可以将所有数字加 +1 吗?或者这会导致国内调用者产生奇怪的费用吗?

如果是这样,我是否需要添加偏好设置,或者是否有一些可靠本地化或地理信息可供我在需要时自动添加 +1?

最佳答案

在处理国际电话号码时,您必须使用所谓的 E164 格式,该格式描述了每个国家/地区电话号码的国际格式。在详细介绍之前,我想说,如果您在某个国家/地区(在本例中为美国),那么您是否添加 +1 并不重要,因为运营商会在调用电话时自动添加它。但当您前往世界各地时,为了安全起见,您需要采用 E164 格式。

我个人在我的一个应用程序中需要这样的东西,我做了一个个人函数,将电话号码作为字符串和国家/地区代码(也作为字符串)并返回该号码的 E164 格式。

为什么我要做这个功能?因为正如您所知,在设备上您可能会看到存储在不同布局中的数字,例如:

  • (02)1234 5678
  • 02 1234 5678
  • 0411 123 123
  • 1 300 123 123
  • 1300 123 123
  • 02-1234-5678
  • 1300-234-234
  • +44 78 1234 1234
  • +44-78-1234-1234
  • 0011 44​​ 78 1234 1234
  • (44) 078 1234 1234

并且您需要先处理它们,然后才能使用它们。我的函数不考虑这种格式的号码:+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/

上一篇:Objective-c PDF 渲染

下一篇:iphone - 不清楚如何从另一个 UIViewController 中正确删除 UIViewController

相关文章:

objective-c - NSDateComponents 问题 - 日期不正确

android - 打 'native vs html5'移动应用之战

iphone - setContentOffset :animated:YES 问题

ios - 以剩余空间为中心制作 View

ios - 导航堆栈之前 View 之间的协议(protocol)委托(delegate)

cocoa - 是否可以在 NSTimer 的间隔内知道当前时间?

iphone - 协议(protocol)中未找到

iphone - 在导航 View Controller 中加载特定场景

ios - Launcher如何获取iPhone的已安装应用程序列表?

ios - 将 UITableView 添加为 subview