Swift 2国家/地区代码的电话号码 [英] Swift 2 country code for a phone number

查看:262
本文介绍了Swift 2国家/地区代码的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从iPhone用户获取国家/地区代码.我不确定是否有关于该国家的信息,以及某些应用程序(例如Viber)如何建议拨号代码.

I am trying to get the country code from iPhone user. I am not sure is there any information about the country and how some applications (like Viber) suggest dial code.

我到这为止了

    let currentLocale = NSLocale.currentLocale()
    let a = currentLocale.localeIdentifier

我在模拟器上对此进行了测试,它返回"en_US".有人可以建议我如何基于此获取国家/地区的拨号代码(或至少将国家/地区代码,我将其发送到服务器以获取拨号代码).

I tested this on simulator and it returns "en_US". Can someone advice how should I get dial code for a country based on this (or at least country code, which I will send to server to get dial code).

谢谢!

推荐答案

根据Pekka的建议,这是可以加载国家/地区标识符的方式:

Based on suggestion from Pekka, this is how country identifier can be loaded:

   // Setup the Network Info and create a CTCarrier object
   let networkInfo = CTTelephonyNetworkInfo()
   let carrier = networkInfo.subscriberCellularProvider

   // Get carrier name
   let countryCode = carrier?.isoCountryCode
   return countryCode

此外,导入CoreTelephony类很重要

import CoreTelephony

之后,可以从 http://country.io/data/

另外请注意,由于它不提供运营商,因此无法在iOS模拟器上使用.

Also be aware that this will not work on iOS simulator, since it doesn't provide carrier.

这篇关于Swift 2国家/地区代码的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆