检索iOS中特定国家/地区的国家/地区电话号码

时间:2011-12-21 10:27:25

标签: ios phone-number

我对iOS开发很新,我正在构建我的第一个应用程序。 我正在尝试使用文本字段自动填充特定国家/地区的电话国家/地区代码。

因此,例如,如果用户选择“UK”,他会自动将“+44”插入该文本字段。

目前,我正在努力寻找如何获得该国家的确切国家电话代码。 我可以创建一个包含所有国家和国家电话代码的NSDictionary,但我认为可能有更好的方法。

4 个答案:

答案 0 :(得分:7)

如果您的目标是获取用户当前位置的拨号代码,那么您应该使用HMDiallingCode

它使用CoreLocation和反向地理编码来获取用户的当前国家/地区,然后检索它的拨号代码。

答案 1 :(得分:4)

我认为,您只能使用CoreTelephony框架获取当前运营商的国家/地区代码:

CTTelephonyNetworkInfo *info = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = info.subscriberCellularProvider;

NSLog(@"country code is: %@", carrier.mobileCountryCode);

如果您需要所有国家/地区的完整代码列表,则需要使用一些在线服务进行查询。

答案 2 :(得分:1)

我担心没有更好的方法。我的应用程序包含一个.plist文件,其中包含每个国家/地区的数组,其中包含名称,代码,电话代码,中继代码等。您将无法从iOS API获取所有信息。 如果您只需要国家/地区代码的国际电话代码,here您就是一个完整的表格链接。

答案 3 :(得分:0)

我为国家代码写了一个开关

let regionCode = NSLocale.current.regionCode
var countryCode:String{
        switch regionCode {
        case "CA", "US", "AG", "AI", "AS", "BB", "BM", "BS", "DM", "DO", "GD", "GU", "JM", "KN", "KY", "LC", "MP", "MS", "PR", "SX", "TC", "TT", "VC", "VG", "VI", "UM": return "+1"
        case "GR": return "+30"
        case "NL": return "+31"
        case "BE": return "+32"
        case "FR": return "+33"
        case "ES": return "+34"
        case "HU": return "+36"
        case "GI": return "+350"
        case "PT": return "+351"
        case "LU": return "+352"
        case "IE": return "+353"
        case "IS": return "+354"
        case "MT": return "+356"
        case "CY": return "+357"
        case "FI","AX": return "+358"
        case "BG": return "+359"
        case "LT": return "+370"
        case "LV": return "+371"
        case "EE": return "+372"
        case "MD": return "+373"
        case "BY": return "+375"
        case "AD": return "+376"
        case "MC": return "+377"
        case "SM": return "+378"
        case "VA": return "+379"
        case "UA": return "+380"
        case "RS": return "+381"
        case "ME": return "+382"
        case "XK": return "+383"
        case "HR": return "+385"
        case "SI": return "+386"
        case "BA": return "+387"
        case "EU": return "+388"
        case "MK": return "+389"
        case "IT": return "+39"
        case "RO": return "+40"
        case "CH": return "+41"
        case "CZ": return "+420"
        case "SK": return "+421"
        case "LI": return "+423"
        case "AT": return "+43"
        case "UK","GG", "IM", "JE": return "+44"
        case "DK": return "+45"
        case "SE": return "+46"
        case "NO","SJ","BV": return "+47"
        case "PL": return "+48"
        case "DE": return "+49"
        case "FK", "GS": return "+500"
        case "BZ": return "+501"
        case "GT": return "+502"
        case "SV": return "+503"
        case "HN": return "+504"
        case "NI": return "+505"
        case "CR": return "+506"
        case "PA": return "+507"
        case "PM": return "+508"
        case "HT": return "+509"
        case "PE": return "+51"
        case "MX": return "+52"
        case "CU": return "+53"
        case "AR": return "+54"
        case "BR": return "+55"
        case "CL": return "+56"
        case "CO": return "+57"
        case "VE": return "+58"
        case "GB","BL","MF": return "+590"
        case "BO": return "+591"
        case "GY": return "+592"
        case "EC": return "+593"
        case "GF": return "+594"
        case "PY": return "+595"
        case "MQ": return "+596"
        case "SR": return "+597"
        case "UY": return "+598"
        case "BQ", "CW": return "+599"
        case "MY": return "+60"
        case "AU", "CX", "CC": return "+61"
        case "ID": return "+62"
        case "PH": return "+63"
        case "NZ", "PN": return "+64"
        case "SG": return "+65"
        case "TH": return "+66"
        case "TL": return "+670"
        case "NF", "AQ", "HM": return "+672"
        case "BN": return "+673"
        case "NR": return "+674"
        case "PG": return "+675"
        case "TO": return "+676"
        case "SB": return "+677"
        case "VU": return "+678"
        case "FJ": return "+679"
        case "PW": return "+680"
        case "WF": return "+681"
        case "CK": return "+682"
        case "NU": return "+683"
        case "WS": return "+685"
        case "KI": return "+686"
        case "NC": return "+687"
        case "TV": return "+688"
        case "PF": return "+689"
        case "TK": return "+690"
        case "FM": return "+691"
        case "MH": return "+692"
        case "XT": return "+800"
        case "XS": return "+808"
        case "JP": return "+81"
        case "KR": return "+82"
        case "RU", "KZ": return "+7"
        case "VN": return "+84"
        case "KP": return "+850"
        case "HK": return "+852"
        case "MO": return "+853"
        case "KH": return "+855"
        case "LA": return "+856"
        case "CN": return "+86"
        case "XN": return "+870"
        case "XP": return "+878"
        case "BD": return "+880"
        case "XG": return "+881"
        case "XV": return "+883"
        case "TW": return "+886"
        case "XD": return "+888"
        case "TR","CT": return "+90"
        case "IN": return "+91"
        case "PK": return "+92"
        case "AF": return "+93"
        case "LK": return "+94"
        case "MM": return "+95"
        case "MV": return "+960"
        case "LB": return "+961"
        case "JO": return "+962"
        case "SY": return "+963"
        case "IQ": return "+964"
        case "KW": return "+965"
        case "SA": return "+966"
        case "YE": return "+967"
        case "OM": return "+968"
        case "PS": return "+970"
        case "AE": return "+971"
        case "IL": return "+972"
        case "BH": return "+973"
        case "QA": return "+974"
        case "BT": return "+975"
        case "MN": return "+976"
        case "NP": return "+977"
        case "XR": return "+979"
        case "IR": return "+98"
        case "XC": return "+991"
        case "TJ": return "+992"
        case "TM": return "+993"
        case "AZ": return "+994"
        case "GE": return "+995"
        case "KG": return "+996"
        case "UZ": return "+998"
        default: return "+"
        }
    }

某些国家/地区没有加入