Retrieving a country telephone code for a specific country in iOS

前端 未结 4 710
深忆病人
深忆病人 2021-02-04 21:35

I\'m quite new to iOS development and I\'m currently building my first app. I\'m trying to get a text field to automatically populate the telephone country code for a specific c

相关标签:
4条回答
  • 2021-02-04 21:53

    I'm afraid there is not a better way. My app include a .plist file with the array of countries with name, code, phone code, trunk code, etc for each. You will not get all that info from iOS API. If you only need the international phone code for a country code, here you are a link with a complete table.

    0 讨论(0)
  • 2021-02-04 21:58

    I wrote a switch for countries code

    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 "+"
            }
        }
    

    Some countries are not in the switch

    0 讨论(0)
  • 2021-02-04 22:00

    I think, you can only get country code for the current Carrier using CoreTelephony framework:

    CTTelephonyNetworkInfo *info = [CTTelephonyNetworkInfo new];
    CTCarrier *carrier = info.subscriberCellularProvider;
    
    NSLog(@"country code is: %@", carrier.mobileCountryCode);
    

    If you need a full list of codes for all countries, you need to use some online service for querying it.

    0 讨论(0)
  • 2021-02-04 22:05

    If your goal is to get the dialling code of the user's current location then you should use HMDiallingCode.

    It uses CoreLocation and reverse geocoding to get current country of the user and then retrieve it's dialling code.

    0 讨论(0)
提交回复
热议问题