Obtaining a country code for a specific country in iOS

I am very new to iOS development, and now I am creating my first application. I am trying to get a text box to automatically fill in the phone code of the phone for a specific country.

So, if, for example, the user selects "UK", he automatically adds "+44" to this text box.

I am currently struggling to find how to get the exact country phone code for the country. I could create an NSDictionary with all countries and a country phone code, but I thought there might be a better way.

+4
source share
4 answers

If your goal is to get a dialing code for the user's current location, you should use HMDiallingCode .

It uses CoreLocation and reverse geocoding to get the current country of the user, and then get the dialing code.

+7
source

I think you can get the country code for the current Carrier statement using the CoreTelephony infrastructure:

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

If you need a complete list of codes for all countries, you need to use some online services to request.

+4
source

I am afraid there is no better way. My application includes a .plist file with an array of countries with name, code, phone code, trunk code, etc. For everybody. You will not get all this information from the iOS API. If you need only the international telephone code for the country code, here , you are a link to the complete table.

+1
source

I wrote a switch for the country 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 not in the switch

0
source

Source: https://habr.com/ru/post/1387355/


All Articles