国家code和区号计算

因为项目中要用到这个功能。实现类似微信注冊时能够选择国家并得到对应的区号。还要推断号码正确与否的正则。

找到了

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

posted @ 2016-04-18 08:45  blfshiye  阅读(475)  评论(0编辑  收藏  举报