国家code和区号计算

117 篇文章 0 订阅
73 篇文章 1 订阅

由于项目中要用到这个功能。实现类似微信注册时可以选择国家并得到相应的区号,还要判断号码正确与否的正则。

找到了

libPhoneNumber-iOS 标准化电话号码库 https://github.com/me2day/libPhoneNumber-iOS
这个类库,可以方便自己来使用。

下面是我在项目中的具体使用:

1.首先定义了2个数组

<span style="font-size:18px;">countryNames = [NSArray arrayWithObjects:@"中国",@"香港地区",@"澳门地区",@"台湾地区",@"美国",@"日本", nil];
countryCodes = [NSArray arrayWithObjects:@"CN",@"HK", @"MO", @"TW", @"US",@"JP", nil];</span>

在已知code情况下,可以通过

- (NSNumber*)getCountryCodeForRegion:(NSString*)regionCode;

这个方法来实现对应的区号

2.在cell来实现

<span style="font-size:18px;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (nil== cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    cell.textLabel.text =  [countryNames  objectAtIndex:indexPath.row];
    NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
    NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
    cell.detailTextLabel.text = [numberFormatter stringFromNumber:[phoneUtil getCountryCodeForRegion:[countryCodes objectAtIndex:indexPath.row]]];
    return cell;
}</span>

这里要把NSNumber转成NSString

最后感谢Norn

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值